I made a SQLite highscores that has 3 columns; id(int – autoincrement), score(long) and percentage(int). I am trying to pull all the data from the columns and compare them to the score the user just got to see if their new score made the highscores list that holds the top 10.
I want the highscores to be prioritized by percentage first and then score to break any ties. Below is where I am starting to do this. I have never used SQLite even outside of Java so this is all brand new to me. I am getting an error. LogCat is below.
Thank you in advance for your help.
//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 output
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)
If you see that the algorithm probably won’t work once the problem is corrected, I would appreciate that being point out too! Thank you in advanced.
Your query for cursor 1 is incorrect, try the following,
You should also do a null check on your cursors before using them, as well as close them when you’re done: