SQLite高分数据库,illegalStateException - Android(java)

时间:2012-12-27 19:37:05

标签: java android sqlite illegalstateexception

我制作了一个有3列的SQLite高分; id(int - autoincrement),score(long)和percentage(int)。我试图从列中提取所有数据,并将它们与用户刚刚看到的分数进行比较,看看他们的新分数是否达到了前10名的高分榜。

我希望首先根据百分比优先考虑高分,然后打分以打破任何关系。以下是我开始这样做的地方。我甚至从未在Java之外使用过SQLite,所以这对我来说都是全新的。我收到了一个错误。 LogCat在下面。

提前感谢您的帮助。

//Check if new record makes the top 10.
public boolean check(long score, int percentage) {
    Cursor c1 = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + " WHERE " + PERCENTAGE + "= percentage;", null);
    Cursor c2 = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);
    if(c1.getCount() > 0) {               
        c2.moveToFirst();
        int i = 0;
        do {
            i++;
            long x = c2.getLong(c2.getColumnIndex("SCORE"));  //Line 39
            if(x < percentage) {
                if(c2.getCount() == 9) {
                    //Delete last record in high score and insert at index.
                    db.rawQuery("DELETE FROM " + TABLE + " WHERE " + ID + " = 10;", null);
                    db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
                    return true;
                } else {
                    //No deletion - just insert.
                    db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
                    return true;
                }
            } else {
                return false;
            }
        } while (c2.moveToNext());
    } else {
        return false;
    }
}

LogCat输出

12-19 02:26:45.270: E/AndroidRuntime(24809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.os.Looper.loop(Looper.java:137)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at dalvik.system.NativeStart.main(Native Method)
12-19 02:26:45.270: E/AndroidRuntime(24809): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.database.CursorWindow.nativeGetLong(Native Method)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.database.CursorWindow.getLong(CursorWindow.java:507)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at com.example.test.DatabaseHelper.check(DatabaseHelper.java:39)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at com.example.test.Results.showResults(Results.java:102)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at com.example.test.Results.onCreate(Results.java:51)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.Activity.performCreate(Activity.java:5008)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-19 02:26:45.270: E/AndroidRuntime(24809):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

如果您发现一旦问题得到解决后该算法可能无法正常工作,我将非常感谢您指出!先谢谢你。

3 个答案:

答案 0 :(得分:2)

您对游标1的查询不正确,请尝试以下操作

Cursor c1 = db.rawQuery( "SELECT " + PERCENTAGE + " FROM " + TABLE + " WHERE " + PERCENTAGE + "=" + percentage, null);

在使用光标之前,您还应对光标进行空检查,并在完成后关闭它们:

if ( c1 != null && c2 != null )
{
    // Do your thing
    /* Stuff */

    // Now close the cursors
    c1.close();
    c2.close();
}

答案 1 :(得分:1)

更改

Cursor c1 = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE +
                                 " WHERE " + PERCENTAGE + "= percentage;", null);

Cursor c1 = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + 
                                " WHERE " + PERCENTAGE + "="+ percentage, null);

目前您没有通过WHERE子句传递百分比

答案 2 :(得分:0)

您获得异常的原因是您正在检查c1是否包含元素(c1.getCount()&gt; 0)然后使用可能为空的c2而不检查c2.moveToFirst()是否未返回假的。

http://developer.android.com/reference/android/database/Cursor.html#moveToFirst()