0

I have an SQLite database where I have a table named object_zone. This table has two columns, of type integer: object and zone.

If I copy the database onto my computer, and query it with:

SELECT object FROM object_zone WHERE zone = 791

it returns several rows.

However, on my Android phone, if I query it with:

Cursor cursor = database.query( "object_zone", new String[] {"object"}, "zone = ?", new String[] { Long.toString( zoneId )}, null, null, null);

Where zoneId = 791, the cursor contains 0 rows. How have I stuffed up the usage of the query function?

2 Answers 2

1

Did you call cursor.moveToFirst() on the returned cursor? Without having called that, cursor.getCount() may erroneously return 0 (or perhaps it's -1...)

I've been tripped up by this a few times...

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

2 Comments

The trouble is, I have a loop right after that query that iterates over the rows in the cursor, using cursor.moveToNext(), and the loop does not execute at all, (i'm using the boolean return as the loop condition, i.e. while( cursor.moveToNext() ). As far as I can tell, it's returning no rows. The weird part is that I use the same construct earlier in my code for a different query and it works fine, the only differences being that that query has no WHERE clause, and it's querying a different table.
@Ogre: Please add that code to the question. If you're only calling moveToNext(), and not starting by calling moveToFirst(), then I think the results are undefined. (I could be wrong about this, but I think the call to moveToNext() is needed.)
0

Maybe the problem here is the casting to string. Can you try the rawQuery method with the same sql that you executes on your computer?

2 Comments

Hmm, using rawQuery, I gave it "SELECT object FROM object_zone WHERE zone = " + Long.toString( zoneId ) as the first parameter, null as the second, and it's working now. Something weird is happening with the substitution of ? for the string array elements though.
I think is related to the cast, try to use an int array.

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.