如何從SQLite中的行中獲取單個值?例如我有getLevel()方法和一個「id,coin,level」的行,getLevel()用於獲取並返回levelonly。我怎樣才能做到這一點?這是我的代碼。Android SQLite檢索整數
public int getLevel(int id) {
int level = 0;
String selectQuery = "SELECT "+colCurrentLvl+" FROM " + userTable + " WHERE " +userID + "="+id;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (null != cursor && cursor.moveToFirst()) {
level = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colCurrentLvl)));
}
cursor.close();
db.close();
return level;
}
而在我的主類我需要設置它像這樣。
int level = db.getLevel(1);
但我的模擬器部隊關閉。任何幫助?
> 10-10 11:09:43.626: E/AndroidRuntime(856): FATAL EXCEPTION: main
10-10 11:09:43.626: E/AndroidRuntime(856): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidexercises.pics1song/com.androidexercises.pics1song.Game}: java.lang.NullPointerException
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.os.Looper.loop(Looper.java:137)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-10 11:09:43.626: E/AndroidRuntime(856): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 11:09:43.626: E/AndroidRuntime(856): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 11:09:43.626: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-10 11:09:43.626: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-10 11:09:43.626: E/AndroidRuntime(856): at dalvik.system.NativeStart.main(Native Method)
10-10 11:09:43.626: E/AndroidRuntime(856): Caused by: java.lang.NullPointerException
10-10 11:09:43.626: E/AndroidRuntime(856): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
10-10 11:09:43.626: E/AndroidRuntime(856): at com.androidexercises.pics1song.DatabaseHelper.getLevel(DatabaseHelper.java:51)
10-10 11:09:43.626: E/AndroidRuntime(856): at com.androidexercises.pics1song.Game.<init>(Game.java:39)
10-10 11:09:43.626: E/AndroidRuntime(856): at java.lang.Class.newInstanceImpl(Native Method)
10-10 11:09:43.626: E/AndroidRuntime(856): at java.lang.Class.newInstance(Class.java:1319)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
10-10 11:09:43.626: E/AndroidRuntime(856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
這是我如何初始化數據庫和關卡。
public class Game extends Activity {
DatabaseHelper db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
db = new DatabaseHelper(Game.this);
level = db.getLevel(1);
}
}
哦,我不需要添加新的行。我只需要每次更新同一行。
什麼是'colCurrentLvl'? – lub0v
字符串包含我級別的列名稱。 – jayyyyy
要求發佈實際錯誤和堆棧跟蹤是否太多了? – njzk2