2014-09-21 116 views
0

我有一個sqlitedatabase我在我的應用程序中實現,我在啓動時不斷收到IllegalStateException。遊標,SQLite和查詢

09-21 22:48:26.749 15762-16277/nz.co.exium.panther E/AndroidRuntime﹕ FATAL EXCEPTION: SyncAdapterThread-2 
java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
     at android.database.CursorWindow.nativeGetString(Native Method) 
     at android.database.CursorWindow.getString(CursorWindow.java:438) 
     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
     at android.database.CursorWrapper.getString(CursorWrapper.java:114) 
     at nz.co.exium.panther.sync.PantherSyncAdapter.getRegistrationId(PantherSyncAdapter.java:269) 
     at nz.co.exium.panther.sync.PantherSyncAdapter.onPerformSync(PantherSyncAdapter.java:64) 
     at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254) 

我相信這是由查詢返回相當空表(除了被自動增量_ID)引起的,然後當我嘗試從光標的字符串。

String registrationID = ""; 
    Uri uri = CustomerEntry.CONTENT_URI; 
    Cursor cursor = mContext.getContentResolver().query(uri, CUSTOMER_PROJECTION, null, null, null); 
    if (cursor.moveToFirst()) { 

     registrationID = cursor.getString(INDEX_CUST_REGID); 
    } 

SQL創建表調用:

final String SQL_CREATE_CUSTOMER_TABLE = "CREATE TABLE " + CustomerEntry.TABLE_NAME + " (" + 
      CustomerEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      CustomerEntry.COLUMN_CUST_NAME + " TEXT NOT NULL, " + 
      CustomerEntry.COLUMN_CUST_EMAIL + " TEXT NOT NULL, " + 
      CustomerEntry.COLUMN_CUST_QRHASH + " TEXT NOT NULL," + 
      CustomerEntry.COLUMN_CUST_REGID + " TEXT NOT NULL)"; 

在這種情況下,INDEX_CUST_REGID是最終靜態INT相關於字符串[]投影的位置,第三在這種情況下的位置。這是有道理的,但有沒有一種方法或方法來查詢SyncAdapter的特定列,如CUST_REGID或方法來檢查請求的列是否在嘗試cursor.getString()之前返回?

這也可以解決另一個問題,我知道EMAIL,名稱,QRHASH之前保存註冊ID。不能只插入一列而沒有其他值(NOT NULL),即使這將是無價值的數據並儘快覆蓋。

嘗試存儲註冊ID的方法。根據要求

private void storeRegistrationID(Context context, String regID) { 
    //DB insert regid 
    ContentValues cValues = new ContentValues(); 
    cValues.put(CustomerEntry.COLUMN_CUST_NAME, ""); 
    cValues.put(CustomerEntry.COLUMN_CUST_EMAIL, ""); 
    cValues.put(CustomerEntry.COLUMN_CUST_QRHASH, ""); 
    cValues.put(CustomerEntry.COLUMN_CUST_REGID, regID); 
    mContext.getContentResolver().insert(CustomerEntry.CONTENT_URI, cValues); 
} 

投影:

String[] CUSTOMER_PROJECTION = new String[]{ 
     CustomerEntry._ID, 
     CustomerEntry.COLUMN_CUST_NAME, 
     CustomerEntry.COLUMN_CUST_EMAIL, 
     CustomerEntry.COLUMN_CUST_QRHASH, 
     CustomerEntry.COLUMN_CUST_REGID 
}; 
+0

顯示投影。 – 2014-09-21 12:37:24

回答

1

你確定表實際上是創造出來的?從你的SQL_CREATE_CUSTOMER_TABLE文本字符串的末尾開始分號分隔 - 雖然我不確定是否必須運行sql來創建表格。我會建議在Eclipse模擬器上運行它,然後從DDMS的角度來看,將數據庫的副本放在一個可以用Firefox的SQLite Manager插件打開它的地方 - 這會顯示數據庫表。

+0

好吧,你肯定在那裏發現了一個錯誤。我將看到我可以使用Android Studio進行數據庫調試。 – fakataha 2014-09-21 22:35:30