Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Sqlite IN, NOT IN syntax

Tags:

I'm trying to run NOT IN select where NOT IN list is dynamic. Something like

SELECT id, type FROM CONTACTS where type NOT IN ('connect','answer') 

In the code my futile attempts were:

db.query(TABLE, new String[] { "id", ""}, " type NOT IN (?)", "connect,answer",      null, null, null); // didn't work db.query(TABLE, new String[] { "id", ""}, " type NOT IN (?)", "'connect','answer'",  null, null, null); // didn't work 

My take on this is that ? substitution treats my comma list as single argument. I did find a solution but it is rather ugly so I won't post it here until someone comes forward with something more elegant

like image 513
Bostone Avatar asked Jan 16 '11 18:01

Bostone


People also ask

How do you write not equal to in SQLite?

In SQLite, you can use the <> or !=

Can I use SQL in Android Studio?

Android App Development for Beginners SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.


1 Answers

You cannot place just one '?' instead of a list of values. As such, there is little to gain from trying to parametrize lists. One can, of course, create 2,4,16-value prepared statements ..." type NOT IN (?,?)", new String[]{ "connect","answer" },... but even on a server with remote RDBMS it has questionable value. Instead, do

db.query(TABLE, new String[] { "id", ""}, " type NOT IN ('connect','answer')",       null, null, null, null); 
if the list is dynamic, you will have to escape the strings and put them into single quoted list.
like image 53
Alex Pakka Avatar answered Oct 04 '22 08:10

Alex Pakka