0

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.

1
  • what red line? You have missed some of these -> } ?? Commented Jun 28, 2012 at 9:32

5 Answers 5

1
LIKE '%"L"%'"

Should be

LIKE '%L%'"
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Barrel that solved the red line problem. I knew it had to be simple. It now runs but the pointer points to an index that is out of bounds, but I'll try and figure out why. thanks again
Thanks everyone for the help. The code looks like this and works perfectly: cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME +" WHERE "+LAST+" LIKE '%L%'", null); cursor.moveToFirst(); THESTRING =cursor.getString(cursor.getColumnIndex(LAST)); Thanks again everyone.
Glad I could help.It would be great if you can mark this post as answered as well :-)
How do I make it as answered?
Just below the arrow flashes that allow you to give points, you can also accept an answer
0

It should be:

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

with the + around L variable..

Comments

0

try this: you forget the +

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

Comments

0

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);

2 Comments

Thanks Uku, I knew it had to be simple. I ran it and got an out of bounds error, when I tried to read what the cursor points, but I'm happy with getting % % problem solved. Thanks again.
This is the best answer because it gives both cases:1: when L is a variable and 2: when L is an actual value.
0
|..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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.