我在登錄後嘗試獲取用戶詳細信息時出現問題。我不知道問題是出自數據庫本身還是使用光標出現問題。數據庫中存在光標問題
這裏是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
@Ahmed同樣的問題,沒有任何變化 –
一段時間,而不是做... while語句不需要if語句。 –
@ Code-Guru moveToFirst()將光標移動到第一行,如果您只需要調用while(cursor.moveToNext()),它會起作用嗎?我只有在至少有一行時才能調用cursor.moveToNext(),另一方面可以使用moveToFirst()來確定。還是我誤會了? – Ahmad