如何从 SQLite 中的一行中获取单个值?例如,我有方法 getLevel() 和一行带有“id、coin、level”的行,getLevel() 用于获取和返回 levelonly。我怎样才能做到这一点?这是我的代码。
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);
}
}
哦,我不需要添加新行。我只需要每次更新同一行。