Search code examples
androiddatabasesqlitedatabase-cursor

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 + "))"

Solution

  • 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..