Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor returns a count of -1, but items are present?

Tags:

android

I am using an SQLite database to store and retreive my app data, and what to check for duplicate entries. I attempt to retrieve all entries where the titles match, as such:

Cursor c = mDb.query(DatabaseHelper.GOALS_TABLE_NAME, 
                     new String[] { Goals.GOAL_ID, Goals.TITLE }, 
                     Goals.TITLE + "='" + title + "'", null, null, null, 
                     null, null);

where title is the one to compare against.

This query runs, but the cursor gives a count of -1. A call without the where clause also returns -1, but I know data is present, as I am able to bind a list view to it.

Is there something I am missing, do I have to populate the cursor somehow?

Thanks in advance,

Venatu

like image 948
Venatu Avatar asked Aug 25 '10 20:08

Venatu


1 Answers

When you do a query(), the Cursor is returned immediately. The query itself is not yet run. Only when you do something that requires a data load will the query be executed. Try executing another method first (e.g., moveToFirst()) before calling getCount() and see if that changes things.

like image 198
CommonsWare Avatar answered Sep 20 '22 13:09

CommonsWare