Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...