pballs
pballs

Reputation: 313

Android database (SQLite) returns a non-empty cursor from an empty table

I verified using SQLite Database Browser that the table contains no rows. I stepped through the query builder code to get the generated query and ran the query in SQLite Database Broswer. The query returned zero rows. But still, the cursor returned by Android's SQLiteQueryBuilder.query method returns true on cursor.moveToFirst() call and returns null values.

Anyone seen something like this before?

Upvotes: 4

Views: 2414

Answers (3)

pballs
pballs

Reputation: 313

OK: I figured this is because I'm using a MAX aggregation function in the query. This could be a bug, may be? I now use a sort with a limit clause instead of MAX and worked around.

Upvotes: 4

Lior
Lior

Reputation: 7915

moveToFirst is actually implemented as moveToPosition(0). moveToPosition is a final method defined in AbstractCursor. Looking at the code, you can see that the result is partially depentandt on getCount. and it seems that in your case getCount returns a non-zero value.

See what's the value of the '_id' column, and try to delete it. Alternatively, try to call that code after re-creating the database (calling 'drop table' and then 'create table').

Upvotes: 0

Heiko Rupp
Heiko Rupp

Reputation: 30944

I think you should do something like:

if (c.getCount()>0) {
    c.moveToFirst();

Upvotes: 0

Related Questions