Android SQLite检索整数

时间:2013-10-10 10:37:02

标签: android sqlite

如何从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);
    }
}

哦,我不需要添加新行。我只需要每次更新同一行。

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

level = cursor.getInt(cursor.getColumnIndex(colCurrentLvl));

答案 1 :(得分:0)

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 

...

10-10 11:09:43.626: E/AndroidRuntime(856): at com.androidexercises.pics1song.Game.(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) 

您过早尝试从数据库初始化活动类成员变量,例如在构造函数中。但在Context之前,您无法将活动用作onCreate()。将成员变量初始化移动到onCreate()。无论如何,活动不应该需要明确的构造函数。