我制作了一个包含 3 列的 SQLite 高分;id(int - autoincrement)、score(long) 和 percent(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)
如果您发现一旦问题得到纠正,该算法可能无法正常工作,我也将不胜感激!提前谢谢你。