Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: SQLite where clause won't return anything
    primarykey
    data
    text
    <p>I am working with SQLite for the past few days and every now and then I get an error I cannot resolve. Sometimes my query works, sometimes it does not, eventhough when I dump the tables, it just should work. Check this dump from Logcat (debugging on device, haven't found a good way to access the db yet...):</p> <p>This is from a table called ct_data</p> <pre><code>01-05 20:13:16.619: I/DataDAO(12453): &gt;===&gt;ID 100 01-05 20:13:16.619: I/DataDAO(12453): &gt;===&gt;cablefinderID 4 01-05 20:13:16.619: I/DataDAO(12453): &gt;===&gt;latitude 51.5077559 01-05 20:13:16.619: I/DataDAO(12453): &gt;===&gt;longitude 7.5959846 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;data DV200SN041385SV4.01TM19:24DT12/04/09CM33ST0BT9MD0SS28UMMDP--- 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;time 1357416761579 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;ID 101 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;cablefinderID 4 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;latitude 51.5077559 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;longitude 7.5959846 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;data DV200SN041385SV4.01TM19:24DT12/04/09CM33ST0BT9MD0SS27UMMDP---DV200SN041385SV4.01TM19:24DT12/04/09CM33ST0BT9MD0SS28UMMDP--- 01-05 20:13:16.629: I/DataDAO(12453): &gt;===&gt;time 1357416771625 </code></pre> <p>And this is the sql statement I also print out (without quotes): </p> <p>"SELECT * from ct_data WHERE cablefinderID='4'"</p> <p>I tried the where clause ='4' with and without quotes (''), however cablefinderID is of type Integer in SQL and long in Java. Here is my Code in Java:</p> <pre><code> String query = "SELECT * from ct_data WHERE cablefinderID='" + registration.getId()+"'"; cursor = database.rawQuery( query, null ); // check if cursor is empty if(!cursor.moveToFirst()) { Log.i(TAG, "WTF!!!"); } </code></pre> <p>I also already tried to use the 2nd argument of rawQuery, String[] selectionArgs namely.</p> <pre><code>String query = "SELECT * from ct_cablefinder_data WHERE cablefinderID=?"; String[] args = {"4"}; cursor = database.rawQuery( query, null ); </code></pre> <p>But still, sometimes I reach "WTF". This occured two days ago and I ended up comparing values in Java later, but that was just a hotfix and not a good thing to go with when there there are hundereds of thousands of rows in the DB lateron. I figured that based on my debugging, the index might have got broken, deleted all App data and then it worked. Now I deleted all App data again and the same stuff happens. I start to doupt on myself here, this makes me crazy...</p> <p>Please tell me there is just a stupid mistake i have overseen!</p> <p>Regards, Simon.</p> <p>---------------------------------EDIT----------------------------------</p> <p>First off, big thanks for the help! But this is crazy. It just won't work. Again, removed all App Data before to get a fresh and clean DB. Check this:</p> <pre><code>01-06 05:54:38.969: I/DataDAO(19394): ====================ct_cablefinder_data==================== 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;ID 1 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;cablefinderID 1 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;latitude 51.5077559 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;longitude 7.5959846 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;data DV200SN041385SV4.01TM05:06DT13/04/09CM33ST0BT9MD0SS00UMMDP--- 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;time 1357451653224 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;ID 2 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;cablefinderID 1 01-06 05:54:39.039: I/DataDAO(19394): &gt;===&gt;latitude 51.5077559 01-06 05:54:39.049: I/DataDAO(19394): &gt;===&gt;longitude 7.5959846 01-06 05:54:39.049: I/DataDAO(19394): &gt;===&gt;data DV200SN041385SV4.01TM05:06DT13/04/09CM33ST0BT9MD0SS00UMMDP--- DV200SN041385SV4.01TM05:06DT13/04/09CM33ST0BT9MD0SS00UMMDP--- DV200SN041385SV4.01TM05:06DT13/04/09CM33ST0BT9MD0SS00UMMDP--- DV200SN041385SV4.01TM05:06DT13/04/09CM33ST0BT9MD0SS00UMMDP--- 01-06 05:54:39.049: I/DataDAO(19394): &gt;===&gt;time 1357451663259 01-06 05:54:39.049: I/DataDAO(19394): ============================================== 01-06 05:54:51.889: I/DataDAO(19394): ====================ct_cableregistration==================== 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;ID 1 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;projectID 1 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;startTime 2013-01-06 05:54:12 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;endTime null 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;deviceType DC200-041385 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;deviceKey 00:01:95:08:2F:3B 01-06 05:54:51.939: I/DataDAO(19394): &gt;===&gt;registrationServerID 0 01-06 05:54:51.949: I/DataDAO(19394): ============================================== And the SQL statement: 01-05 20:14:20.859: E/DataDAO(12453): SELECT * from ct_cablefinder_data WHERE cablefinderID=1 </code></pre> <p>This is my adjusted code, followed the suggestion by Meghal Shah:</p> <pre><code>public List&lt;DataVO&gt; getDataByRegistration( CableRegistrationVO registration ) { synchronized ( lock ) { openReadable(); Cursor cursor = null; List&lt;DataVO&gt; result = new ArrayList&lt;DataVO&gt;(); try { String query = "SELECT * from ct_cablefinder_data WHERE cablefinderID=" + registration.getId(); cursor = database.rawQuery( query, null ); dumpTableI( "ct_cablefinder_data" ); // see tabledump above dumpTableI( "ct_cableregistration" ); // again, dump above Log.e( TAG, query ); while ( cursor.moveToNext() ) { result.add( this.bindSQLite( cursor ) ); } } catch ( Throwable t ) { Log.e( TAG, t.toString() ); } finally { if ( cursor != null ) { cursor.close(); } } close(); return result; } } </code></pre> <p>I mean, I am doing step by step debugging and everytime the program reaches the while condition, it just jumps straight down to the finally block and closes the cursor, retrieving no data whatsoever. the result ArrayList remains empty. But it is there: cablefinderID = 1, I can see it, you can see it, can't you?</p> <p>I now will try to cast everything to Strings and try to compare those in the WHERE clause and if that won't work, well I dont know but heavy alcohol abuse seems considerable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload