java - SQLite returned rows count is zero -
public class sqliteprovider extends sqliteopenhelper { private static sqlitedatabase sqlitedatabase ; public sqliteprovider ( context context ) { super ( context , database_name , null , database_version ) ; } @override public void oncreate ( sqlitedatabase sqlitedatabase ) { try { . sqlitedatabase = . getwriteabaledatabase ( ) ; . sqlitedatabase . execsql ( table_create ) ; . sqlitedatabase . close ( ) ; } catch ( exception exception ) { log . ( log_tag , "exception `" + exception . tostring ( ) + "`" ) ; } } private void insert ( string column1value , string column2value ) { try { sqlitedatabase = . getwritabledatabase ( ) ; sqlitedatabase . execsql ( query_to_insert ) ; sqlitedatabase . close ( ) ; } catch ( exception exception ) { log . ( log_tag , "exception `" + exception . tostring ( ) + "`" ) ; } } private string selectcolumn1bycolumn2 ( string column2value ) { try { sqlitedatabase . getwritabledatabase ( ) ; cursor = sqlitedatabase . rawquery ( select_query ) ; sqlitedatabase . close ( ) } catch ( exception exception ) { log . ( log_tag , "exception `" + exception . tostring ( ) + "`" ) ; } if ( cursor . getcount ( ) > 0 ) { return cursor . getstring ( 0 ) ; } else { log . ( log_tag , "selectcolumn1bycolumn2 row count 0 " ) ; return ""; } } public string doit ( ) { insert ( variable1 , variable2 ) ; return selectcolumn1bycolumn2 ( variable2 ) ; } }
all methods use same sqlitedatabase object , each of calls getwritabledatabase in beginning. function doit ( ) return empty string , log row count 0 ( selectcolumn1bycolumn2 row count zero
) , no 1 exception. transaction , without , both of not work how right?
update april 8
i have done
add block sqlitedatabase . close ( )
add cursor . close ( )
when necessary before sqlitedatabase . close ( )
change cursor . getstring ( 0 )
cursor . getstring ( cursor . getcolumnindex ( table_column1 ) )
change if ( cursor . getcount ( ) > 0 )
if ( cursor . movetofirst ( ) )
and returns row count 0 , don`t work on jelly bean 4.2 - android application crashes
the reason why not getting desired result because closing database first , reading cursor.
if first read cursur, , when finished using cursor close database, should work.
the user named alibi gives explanation in answer: clicky
1) cursor pointer data returned query, doesn't contain data query. increase performance/efficiency (large resultsets aren't read @ once -> less memory used). therefore, if close database, cursor can't retrieve data -> it's empty.
2) when close cursor, associated resources released -> can't access data associated cursor (since has been released), can make new queries using or other cursors. when close database, can't query anymore (until re-open it).
3) close cursors. otherwise run problems - gc complain if cursor isn't closed , new queries blocked.
4) if close when app finishes, yes
again, credit goes alibi.
update
private string selectcolumn1bycolumn2 ( string column2value ) { try { sqlitedatabase . getwritabledatabase ( ) ; cursor = sqlitedatabase . rawquery ( select_query ) ; cursor.movetofirst(); if ( cursor . getcount ( ) > 0 ) { return cursor . getstring ( 0 ) ; } else { log . ( log_tag , "selectcolumn1bycolumn2 row count 0 " ) ; return ""; } } catch ( exception exception ) { log . ( log_tag , "exception `" + exception . tostring ( ) + "`" ) ; } { cursor.close(); sqlitedatabase . close ( ); } }
cursor.close , sqlitedatabase.close() moved block. ensure database , cursor gets cleaned when done using it.
also added cursor.movetofirst(); call before first call getcount();
not 100% sure it's needed it's worth try.
Comments
Post a Comment