数据库中的光标问题

时间:2012-11-23 20:59:10

标签: android sqlite cursor

我在登录后尝试获取用户详细信息时遇到问题。我不知道问题是来自数据库本身还是使用Cursor的问题。

以下是 代码

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;
}

在活动中:

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"));

但是logcat显示错误:

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

2 个答案:

答案 0 :(得分:1)

改变这个:

 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));
    }

对此:

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()将光标移动到第一行,如果光标为空,则返回false。

答案 1 :(得分:0)

游标中的列索引从零索引开始,就像数组和arraylists一样。这是表格列的样子:

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) " +     ");";

正如您所看到的,当您尝试请求时,没有cursor.getString(11)。这个问题很容易解决。但是您应该知道,您不仅限于通过索引请求列,还可以使用cursor.getString(cursor.getColumnIndex(columnName))按名称请求列,其中columnName是您在创建时分配给列的字符串值表声明。有点冗长,但如果另一个证明很麻烦,它可能会导致更少的错误。