0

This is my code to query my android database.

int lastID = -1;

String selectQuery = "SELECT * FROM " + TABLE_MAIN + " WHERE " + KEY_ID + " > " + lastID;

Log.i(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);

if (c.moveToFirst())
{
    ...
}
else
{
    return null;
}

For some reason every time this runs (I have debugged it) it always falls into the else of the if statement and I dont know why. This led me to believe that my query is wrong but I cannot see how it is wrong. I know that the table name and column name is correct because it is a globally defined variable. Is anyone able to spot my mistake? Thanks

4
  • The most obvious reason would be that your table has no data matching your selection. Commented Jun 8, 2015 at 14:06
  • yeah no I checked that with a database viewer, it has data Commented Jun 8, 2015 at 14:08
  • Sure it was the same database you were looking at? Commented Jun 8, 2015 at 14:08
  • try and remove the condition " WHERE " + KEY_ID + " > " + lastID; and see if it returns any data if it still don't return any data, then im guessing either the table has no data, you using the wrong database or TABLE_MAIN is not the table you expect Commented Jun 8, 2015 at 14:23

1 Answer 1

1

Test your select like

String selectQuery = "SELECT * FROM " + TABLE_MAIN + " WHERE " + KEY_ID + " > " + "'" + lastID + "'";

Some devices (this happen to me in some devices) sqlite integer only works with > 1 without simple quotes, but numbers <= 0 need simple quotes. Why? I don't know.

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

1 Comment

actually this did solve my problem after testing it again...thanks!

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.