Android database query not returning any results

75 views Asked by At

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

1

There are 1 answers

1
Santiago On BEST ANSWER

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.