2015-09-08 87 views
0

我已經實現了一個返回布爾值true的SQL語句,該表中存在用戶名和日期。以下是我的實施。我也包含了顯示在logcat的CursorIndexOutOfBoundsException:請求索引0,大小爲0

public boolean flagExists(String Username, String Date) { 
SQLiteDatabase db = getReadableDatabase(); 
String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + " ' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'"; 

// Add the String you are searching by here. 
// Put it in an array to avoid an unrecognized token error 
Cursor cursor = db.rawQuery(selectString, new String[]{Username,Date}); 

boolean hasObject = false; 
if (cursor.moveToFirst()) { 
    hasObject = true; 

    //region if you had multiple records to check for, use this region. 
    int count = 0; 
    while (cursor.moveToNext()) { 
     count++; 
    } 
    //here, count is records found 
    Log.d(TAG, String.format("%d records found", count)); 

    //endregion 

} 
cursor.close();   // Dont forget to close your cursor 
db.close();    //AND your Database! 
return hasObject; 

}

rocess: com.example.s210121629.myhealth, PID: 10953 
    android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
      at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 
      at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
      at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:86) 
      at com.example.s210121629.myhealth.Database.DBHelper.getPerson(DBHelper.java:400) 
      at com.example.s210121629.myhealth.dashboard.DashboardActivity.decisionAlgorithm(DashboardActivity.java:347) 
      at com.example.s210121629.myhealth.dashboard.DashboardActivity.reloadlist(DashboardActivity.java:327) 
      at com.example.s210121629.myhealth.dashboard.DashboardActivity$3.onFinish(DashboardActivity.java:252) 
      at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 

回答

0

變化

Cursor cursor = db.rawQuery(selectString, new String[]{Username,Date}); 

Cursor cursor = db.rawQuery(selectString, null); 

當前的錯誤,如果你想指定selectionArgs參數,您selectString必須包含一些? s等於給selectionArgs兩個長度String[]

+0

在建議的更改E/SQLiteLog後出現以下錯誤:(1)在「ANDFlagDate」附近:語法錯誤 09-08 21:04:25.569 27790-27790/com.example.s210121629.myhealth E/AndroidRuntime :致命例外:main 進程:com.example.s210121629.myhealth,PID:27790 android.database.sqlite.SQLiteException:near「ANDFlagDate」:語法錯誤(代碼1):編譯時:SELECT * FROM Flags WHERE用戶名='[email protected]'ANDFlagDate= '08 -09-2015' – Tungamirai

+0

您必須在關鍵字之前和之後留出空格AND – Blackbelt

+0

我沒有包含空格 – Tungamirai

0

變化

String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + " ' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'"; 

String selectString = "SELECT * FROM " + TABLE_FLAGS + " WHERE " + COLUMN_FLAGS_USERNAME + " = '" + Username + "' " + " AND " + COLUMN_FLAGS_DATE + "= '" + Date + "'"; 

你加入一個額外的空間來Username變量。

+0

在提出建議更改後出現以下錯誤E/SQLiteLog :(1)在「ANDFlagDate」附近:語法錯誤 09-08 21:04:25.569 27790-27790/com.example.s210121629.myhealth E/AndroidRuntime:致命例外:main 進程:com.example.s210121629.myhealth,PID:27790 android.database.sqlite.SQLiteException:接近「ANDFlagDate」:語法錯誤(代碼1):,編譯時:SELECT * FROM Flags WHERE Username ='[email protected]'ANDFlagDate= '08 -09-2015' at – Tungamirai

+0

你確定你複製並粘貼了嗎? –

+0

我曾經在@之後和之前留下了一個空格,就像@Blackbelt建議的那樣 – Tungamirai

相關問題