Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite .query() method, WHERE clause is only taking double quotes strings

Tags:

android

sqlite

I have variable:

String owner="Mike";
String[] columns ={"quantity", "price","owner"}

My cursor is trying to get
Cursor findEntry = db.query("sku_table", columns, "owner="+owner, null, null, null, null);

I got an error no such column error

android.database.sqlite.SQLiteException: no such column: owner: , while compiling: SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike

But if I take this query:

SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike

and add "" to Mike, and tested in sqlite browsers to execute the query, I do get back the row. The working query looks like this:

SELECT quantity, price, owner, FROM sku_table WHERE owner="Mike"

Can somebody drop some insights about how do I incorporate double quotes? Other than use \" Thanks!

like image 853
sammiwei Avatar asked Feb 07 '12 23:02

sammiwei


2 Answers

Sorry, but that is exactly the reason why you should work with what the method offers! @Leandros and @Jake are helping in the totally wrong direction! Sorry to say that...

The only solution you should use is this:

Cursor findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);

ps: Yes I down voted both answers as they may work but providing a solution that shouldn't be used.

Update:

If you need more than one where condition, just add it like you would do in a normal query

Cursor findEntry = db.query("sku_table", columns, "owner=? and price=?", new String[] { owner, price }, null, null, null);

The order of the ? and the new String[] {...} elements must be the same!

like image 115
WarrenFaith Avatar answered Oct 17 '22 09:10

WarrenFaith


Cursor findEntry = db.query("sku_table", columns, "owner='"+owner+"'", null, null, null, null);
like image 29
Jake Avatar answered Oct 17 '22 09:10

Jake