0

I am having a problem trying to get the user details after signing in. I dont know if the problem is from the Database itself or a problem with using the Cursor.

Here is the CODE

public void onCreate(SQLiteDatabase db) {
    Log.d("SQLite OnCreate", "Creating table");
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("

    + KEY_NAME + " VARCHAR (255), " + KEY_MAIL + " VARCHAR (255), "
            + KEY_PASSWORD + " VARCHAR (255), " + KEY_ID
            + " VARCHAR(255), " + "unique_id " + "VARCHAR (255), "
            + KEY_COURSE1 + " VARCHAR(255), " + KEY_COURSE2
            + " VARCHAR(255), " + KEY_COURSE3 + " VARCHAR(255), "
            + KEY_COURSE4 + " VARCHAR(255), " + KEY_COURSE5
            + " VARCHAR(255), " + KEY_COURSE6 + " VARCHAR(255) " +     ");";
    db.execSQL(CREATE_LOGIN_TABLE);
    Log.d("SQLite OnCreate", "table created");

}

public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToNext();
    if (cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("password", cursor.getString(3));
        user.put("email", cursor.getString(2));
        user.put("regId", cursor.getString(4));
        user.put("unique_id", cursor.getString(5));
        user.put("course1", cursor.getString(6));
        user.put("course2", cursor.getString(7));
        user.put("course3", cursor.getString(8));
        user.put("course4", cursor.getString(9));
        user.put("course5", cursor.getString(10));
        user.put("course6", cursor.getString(11));
    }
    cursor.close();
    db.close();
    // return user
    return user;
}

And in the Activity:

DatabaseStudents dbo = new DatabaseStudents(getApplicationContext());
            HashMap<String, String> details = dbo.getUserDetails();
             course1 = Integer.parseInt(details.get("course1"));
             course2 = Integer.parseInt(details.get("course2"));
             course3 = Integer.parseInt(details.get("course3"));
             course4 = Integer.parseInt(details.get("course4"));
             course5 = Integer.parseInt(details.get("course5"));
             course6 = Integer.parseInt(details.get("course6"));

BUT the logcat shows an error saying:

11-23 22:49:28.051: E/AndroidRuntime(2059): FATAL EXCEPTION: main
11-23 22:49:28.051: E/AndroidRuntime(2059): java.lang.RuntimeException: Unable to start      activity ComponentInfo{guc.edu.iremote/guc.edu.iremote.Main}:     java.lang.IllegalStateException: Couldn't read row 0, col 11 from CursorWindow.  Make sure     the Cursor is initialized correctly before accessing data from it.
11-23 22:49:28.051: E/AndroidRuntime(2059):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.os.Looper.loop(Looper.java:137)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at java.lang.reflect.Method.invoke(Method.java:511)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at dalvik.system.NativeStart.main(Native Method)
11-23 22:49:28.051: E/AndroidRuntime(2059): Caused by: java.lang.IllegalStateException:     Couldn't read row 0, col 11 from CursorWindow.  Make sure the Cursor is initialized     correctly before accessing data from it.
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.database.CursorWindow.nativeGetString(Native Method)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.database.CursorWindow.getString(CursorWindow.java:434)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at guc.edu.iremote.DatabaseStudents.getUserDetails(DatabaseStudents.java:117)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at guc.edu.iremote.Main.onCreate(Main.java:87)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.Activity.performCreate(Activity.java:5008)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-23 22:49:28.051: E/AndroidRuntime(2059):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-23 22:49:28.051: E/AndroidRuntime(2059):     ... 11 more
0

2 Answers 2

1

Change this:

 cursor.moveToNext();
    if (cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("password", cursor.getString(3));
        user.put("email", cursor.getString(2));
        user.put("regId", cursor.getString(4));
        user.put("unique_id", cursor.getString(5));
        user.put("course1", cursor.getString(6));
        user.put("course2", cursor.getString(7));
        user.put("course3", cursor.getString(8));
        user.put("course4", cursor.getString(9));
        user.put("course5", cursor.getString(10));
        user.put("course6", cursor.getString(11));
    }

To this:

if (cursor.moveToFirst()) {
        do {

            user.put("name", cursor.getString(1));
            user.put("password", cursor.getString(3));
            user.put("email", cursor.getString(2));
            user.put("regId", cursor.getString(4));
            user.put("unique_id", cursor.getString(5));
            user.put("course1", cursor.getString(6));
            user.put("course2", cursor.getString(7));
            user.put("course3", cursor.getString(8));
            user.put("course4", cursor.getString(9));
            user.put("course5", cursor.getString(10));
            user.put("course6", cursor.getString(11));
        } while (cursor.moveToNext());
    }

cursor.moveToFirst() moves the cursor to the first row and returns false if the cursor is empty.

Sign up to request clarification or add additional context in comments.

6 Comments

@Ahmed same problem, nothing changed
A while, rather than do...while, statement removes the need for the if statement.
@Code-Guru moveToFirst() moves the cursor to the first row, will it work if you would just call while(cursor.moveToNext())? I tought cursor.moveToNext() could only be called if there is at least one row and that on the other hand could be determined with moveToFirst(). Or am I mistaken?
@Code-Guru also the same problem still exists
@OmArHesham What's on line 87?
|
0

the column indexes in the cursor starts on a zero index much like arrays and arraylists. Here's what your table columns look like:

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
// column index:    + title
0:    + KEY_NAME + " VARCHAR (255), " 
1:    + KEY_MAIL + " VARCHAR (255), "
2:    + KEY_PASSWORD + " VARCHAR (255), " 
3:    + KEY_ID + " VARCHAR(255), "  
4:    + "unique_id " + "VARCHAR (255), "
5:    + KEY_COURSE1 + " VARCHAR(255), " 
6:    + KEY_COURSE2 + " VARCHAR(255), " 
7:    + KEY_COURSE3 + " VARCHAR(255), "
8:    + KEY_COURSE4 + " VARCHAR(255), " 
9:    + KEY_COURSE5 + " VARCHAR(255), " 
10:    + KEY_COURSE6 + " VARCHAR(255) " +     ");";

As you can see, there's is no cursor.getString(11) as you are trying to request. this issue is easy enough to fix. but you should know that you're not limited to requesting a column by its index you can also request it by name with cursor.getString(cursor.getColumnIndex(columnName)), where columnName is the String value that you assigned to the column in your create table statement. a little verbose, but it might lead less errors if the other one proves cumbersome.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.