deimos1988
deimos1988

Reputation: 6086

SQLIte query returns empty cursor

I am trying to get data from my SQLite database in Android using this query.

Cursor cursor = db.rawQuery("SELECT * FROM Task where taskId = 1 ", null);

However the cursor is returned empty, a taskId with value 1 exists.

If I do this

Cursor cursor = db.rawQuery("SELECT * FROM Task", null);

my cursor contains all of the values - including a taskId with value 1.

I have tried all of the following commands as well, neither of them worked:

Cursor cursor = db.rawQuery("SELECT * FROM Task where taskId = " + 1, null);
Cursor cursor = db.rawQuery("SELECT * FROM Task where taskId = " + "'1'", null);
Cursor cursor = db.rawQuery("SELECT * FROM Task where taskId = ?", new String[]{"1"});

taskId is of type Integer, tried it with Text, too, also didn't work.

Is there something I didn't consider? Help would be highly appreciated!

EDIT: Code for creating the database:

CREATE TABLE Task + " (" +
        _id INTEGER PRIMARY KEY," +
        "taskDescription TEXT, +                
        "taskId INTEGER," +
        "taskName TEXT" + 
        "PreviousTaskID " + "REFERENCES " + "PreviousTasks " + 
        "(" + PreviousTasks._ID + "))"

Upvotes: 3

Views: 19237

Answers (4)

dipali
dipali

Reputation: 11188

Cursor cursor = null;
String Query ="SELECT * FROM Task where taskId = 1 ";
cursor = sqldb.rawQuery(Query, null);

if (cursor != null && cursor.moveToFirst()) {
  do {
    // ...
  } while (cursor.moveToNext());

  cursor.close();
}

i hope its useful to you..

Upvotes: 9

user3522383
user3522383

Reputation: 1

If you mean -1 count of elements of cusor by telling "empty", so you just have to use moveToFirst() before.

Cusror c = rawQuery(...); 
// this line is required 
c.moveToFirst();

then if you'd try to invoke

c.getCount()

it returns != 0 (if the query returns more than 0).

Upvotes: 0

deimos1988
deimos1988

Reputation: 6086

I just found out what the problem was: When accessing the cursor, I used following code:

cursor.moveToFirst();
while(cursor.moveToNext()){
  //stuff
};

Instead what I had to do was this:

cursor.moveToFirst();
do{
  //stuff
} while(cursor.moveToNext());

I didn't notice the error when I queried

Cursor.rawQuery("SELECT * FROM TASK");

since my cursor had more than one entry and. But when I queried

Cursor.rawQuery("SELECT * FROM TASK WHERE taskID=1");

the cursor only has one entry which was skipped because of the while-loop.

Upvotes: 1

Smile2Life
Smile2Life

Reputation: 1921

If you created database for example with 4 fields and used this db then if you add new field you need to recreate database on phone, delete and create it.

Your database must be at /data/data/com.yourapp.package/databases/, use this or this open it and check if anything wrong.

Remove all spaces :

Cursor cursor = getReadableDatabase().rawQuery("SELECT  * FROM  Task WHERE taskId=1", null);

Step the cursor to the first result :

cursor.moveToFirst();

Use getInt:

int taskid = c.getnt(c.getColumnIndex("taskId"));

Upvotes: 1

Related Questions