14

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!

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
sammiwei
  • 3,140
  • 9
  • 41
  • 53

6 Answers6

88

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!

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
2

SELECT quantity, price, owner, FROM sku_table WHERE owner='Mike' this is the correct SELECT. You forget the ' ' (single quotes)

Leandros
  • 16,805
  • 9
  • 69
  • 108
2
Cursor findEntry = db.query("sku_table", columns, "owner='"+owner+"'", null, null, null, null);
Jake
  • 2,471
  • 15
  • 24
1
 public Cursor show_vol(String  vol,String bk,String hadnu)
           { 
              SQLiteDatabase db = this.getReadableDatabase();
              String[] columns ={"hadith"};//colums name that you select

              Cursor res = db.query("volume2", columns, "hadithno=?", new String[] { hadnu }, null, null, null);

 //volume2 is table name  and hadithno is colume name  l
//select hadith from volume2 where hadithno=hadnu  //working like s

 1. List item

              return res;
           }
sandhu
  • 305
  • 4
  • 4
0

I know this is an old question, but you can also do it like this:

Cursor findEntry = db.query("sku_table", columns, "owner=\'"+owner+"\'", null, null, null, null);

I just did it in my app and it worked as expected.

Jake's answer was similar, but probably wouldn't work without the \ before the '

Randy
  • 1,068
  • 2
  • 14
  • 32
  • Escaping single quotes in a double-quoted string is possible but not required. – CL. Oct 26 '13 at 07:46
  • @Randy: Why would you answer a question with a wrong answer a *year* after a way better and correct answer was given? You **should** never use your way to create a query. – WarrenFaith Nov 07 '14 at 13:21
  • @WarrenFaith Would you please, for the love of god, just stop saying "It's wrong", and tell us WHY it's wrong? Nobody will listen to a person who just says something is wrong and doesn't give a reason for it... – Timmiej93 Jun 14 '16 at 23:22
  • @Timmiej93 in my answer, which is also the accepted one, is everything explained. As you obviously failed to see that, here a quote: "as they may work but providing a solution that shouldn't be used." – WarrenFaith Jun 15 '16 at 06:04
  • @WarrenFaith Come on dude, that is NOT explaining WHY it shouldn't be used, it's just saying it shouldn't be used. No explanation at all.. – Timmiej93 Jun 15 '16 at 08:10
  • @Timmiej93 first of all: I am not your dude. One other sample: You could do network on main thread. It was so often used that Android introduced an Exception that is thrown when you do it now. They did it because unbelievable many devs did this stupid thing. So it worked, but it was against the frameworks intention. Same here. Use what the framework offers! If this is not a valid argument, just leave it... – WarrenFaith Jun 15 '16 at 08:14
0

the simplest way is to use SELECT col1,col2 FROM table_name WHERE col =' something' ; just like Leandros said , my problem was the single quotes , thnx