Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9035791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:50:24+00:00 2026-06-16T08:50:24+00:00

I made a SQLite highscores that has 3 columns; id(int – autoincrement), score(long) and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:50:25+00:00Added an answer on June 16, 2026 at 8:50 am

    Your query for cursor 1 is incorrect, try the following,

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

    You should also do a null check on your cursors before using them, as well as close them when you’re done:

    if ( c1 != null && c2 != null )
    {
        // Do your thing
        /* Stuff */
    
        // Now close the cursors
        c1.close();
        c2.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a SQLite highscores that has 3 columns; rank(int), score(long) and percentage(int). I
I've made a program that comunicates with a SQLite DB. Now, I'm trying to
I am trying to copy a database that I made with SQLite manager, in
I have a pre-made SQLite database that I am downloading from the net via
I created a SQLite database that currently has only one table. I connected it
I made a database through sqlite in c++. The db has been created in
I've made a web application using SQLite (2.8.17), I've only now discovered that there's
What I am trying to do is to copy the database that I made
I have made an app that has 3 activities. In the fisrt activity(Import) i
I made SQLite database table that stores records, is it possible to change some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.