1
 Cursor cursor = db.rawQuery("SELECT id,no,name,lastname FROM mytable WHERE " +
                                   "id = (SELECT MIN(id) and no=0 FROM mytable ); ",null);

I am getting that error at below, Could you please help me ?

What I want is to retrieve the record that has min Id where no=0 in the mytable....

Error:android.database.sqlite.SQLiteException: near "WHERE": syntax error: , while compiling:

1 Answer 1

3

Without knowing too many specifics about sqlite syntax, I'd guess your subquery isn't formatted quite right.

Try

SELECT id, no, name, lastname FROM mytable WHERE
       id = (SELECT min(ID) FROM mytable WHERE no = 0)

or

SELECT id, no, name, lastname FROM mytable WHERE
       id = (SELECT min(ID) FROM mytable) AND no = 0

depending on what your interpretation was (but it sounds like the first one). The first will return rows with the minimum id with also a no of 0, but the second query will return the minimum id and then use it to select for a row with a no of 0.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 - however, you might want to add an explanation of what the difference is between the two statements to further clarify. :)
Thanks, I've made that edit (which hopefully didn't confuse things further). I also just checked it out in sqlite3 on my local machine and it seems to be correct.
Thanks for your replies, I tried that one, Cursor cursor = db.rawQuery("SELECT id,no,name,lastname FROM mytable WHERE id = (SELECT min(ID) FROM mytable WHERE no = 0);",null); But still, I have an error message.....android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
You should move on to first row in the cursor by calling Cursor cur = db.raqQuery("Select * from myTable",null); if (cur != null) { if (cur.moveToFirst()) { do { cur.getString(cur.getColumnIndex("Name"))); // "Name" is the field name in table } while (cur.moveToNext()); } }
it is because the cursor is initially pointing to row at index -1 but actual row starts from index 0. Calling moveToFirst() will move the cursor to index 0.
|

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.