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 {
this . sqliteDatabase = this . getWriteabaleDataBase ( ) ;
this . sqliteDatabase . execSQL ( TABLE_CREATE ) ;
this . sqliteDatabase . close ( ) ;
} catch ( Exception exception ) {
Log . i ( LOG_TAG , "Exception is `" + exception . toString ( ) + "`" ) ;
}
}
private void insert ( String column1Value , String column2Value ) {
try {
sqliteDatabase = this . getWritableDatabase ( ) ;
sqliteDatabase . execSQL ( QUERY_TO_INSERT ) ;
sqliteDatabase . close ( ) ;
} catch ( Exception exception ) {
Log . i ( LOG_TAG , "Exception is `" + exception . toString ( ) + "`" ) ;
}
}
private String selectColumn1byColumn2 ( String column2Value ) {
try {
sqliteDatabase . getWritableDatabase ( ) ;
cursor = sqliteDatabase . rawQuery ( SELECT_QUERY ) ;
sqliteDatabase . close ( )
} catch ( Exception exception ) {
Log . i ( LOG_TAG , "Exception is `" + exception . toString ( ) + "`" ) ;
}
if ( cursor . getCount ( ) > 0 ) {
return cursor . getString ( 0 ) ;
} else {
Log . i ( LOG_TAG , "selectColumn1byColumn2 row count is zero " ) ;
return "";
}
}
public String doIt ( ) {
insert ( VARIABLE1 , VARIABLE2 ) ;
return selectColumn1byColumn2 ( VARIABLE2 ) ;
}
}
所有方法都使用相同的 sqliteDatabase 对象,并且每个方法都在开始时调用 getWritableDatabase。函数 doIt ( ) 只返回空字符串并记录行数为零 ( selectColumn1byColumn2 row count is zero
) 且没有任何异常。我用事务和不做,它都不起作用我怎么做对?
4月8日更新
我已经做好了
添加 finally 块sqliteDatabase . close ( )
必要时添加cursor . close ( )
之前sqliteDatabase . close ( )
更改cursor . getString ( 0 )
为cursor . getString ( cursor . getColumnIndex ( TABLE_COLUMN1 ) )
更改if ( cursor . getCount ( ) > 0 )
为if ( cursor . moveToFirst ( ) )
现在它返回的行数为零并且不适用于 Jelly Bean 4.2 - android 应用程序崩溃