2012-11-23 82 views
0

我在登錄後嘗試獲取用戶詳細信息時出現問題。我不知道問題是出自數據庫本身還是使用光標出現問題。數據庫中存在光標問題

這裏是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; 
} 

而且在活動時間:

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 

回答

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如果光標是空的。

+0

@Ahmed同樣的問題,沒有任何變化 –

+0

一段時間,而不是做... while語句不需要if語句。 –

+0

@ Code-Guru moveToFirst()將光標移動到第一行,如果您只需要調用while(cursor.moveToNext()),它會起作用嗎?我只有在至少有一行時才能調用cursor.moveToNext(),另一方面可以使用moveToFirst()來確定。還是我誤會了? – Ahmad

0

遊標中的列索引從零索引開始,非常類似於數組和列表列表。這是你的表列的樣子:

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是您分配給create table語句中列的字符串值。有點冗長,但如果另一個證明麻煩,它可能會導致更少的錯誤。