3

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 + "))"
4
  • How about showing some code? Commented Feb 23, 2014 at 18:25
  • Can you show the definition of your table? Commented Feb 23, 2014 at 18:32
  • Pff, still nothing wrong... are you sure you're not mixing the _id field with taskId?? Commented Feb 23, 2014 at 18:48
  • I tried it with both _id and taskId - same result :( Commented Feb 23, 2014 at 18:51

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

1 Comment

FYI, this will fail in case there is no data. You should check the return value of moveToFirst(), too.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.