Peter James
Peter James

Reputation: 352

rawQuery, getting cursor to point to row with STRING in column

I've been learning Java, using Eclipse for Android, for 2 months and have just started learning about Databases. I know I am doing something wrong here and the answer is simple but I just can't get it. And I know this type of question has been asked 1M times before. Sorry to ask it again.

I have a table called TABLE_NAME:

|-----|-------|-------|
|._id.|.ITEM..|.LAST..|
|..1..|...A...|.......|
|..2..|...B...|...L...|<--I want the cursor to point to this row which has "L" in column LAST 
|..3..|...C...|.......|
|-----|-------|-------|

I want the cursor to point to the row with the "L" in the "LAST" column. I am using the following code:

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%"L"%'", null);

but I get a red line under the last bit of my code. Can anyone help?

Just figured out how to answer my own question:

Thanks everyone for the help. After changing '%"L"%'" to '%L%'" the new code is:

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%L%'", null);

The I wrote: cursor.moveToFirst();<--- telling the cursor to go to the row the query found and then:

THESTRING =cursor.getString(cursor.getColumnIndex(LAST));<--- getting the info in the row

Thanks again everyone.

Upvotes: 0

Views: 1066

Answers (5)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

|..2..|...B...|...L...|<--I want the cursor to point to this row which has "L" i

it should be

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%L%'", null);

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42040

There's syntactically this error:

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%"+L+"%'", null);

You are missing two + for concatenation. Or like this if L is an actual value not a variable

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%L%'", null);

Upvotes: 0

idiottiger
idiottiger

Reputation: 5177

try this: you forget the +

 db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + LAST + " LIKE '%" + L + "%'", null);

Upvotes: 0

barrel
barrel

Reputation: 974

LIKE '%"L"%'"

Should be

LIKE '%L%'"

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

It should be:

cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%"+L+"%'", null);

with the + around L variable..

Upvotes: 0

Related Questions