我已經實現了一個返回布爾值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)
在建議的更改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
您必須在關鍵字之前和之後留出空格AND – Blackbelt
我沒有包含空格 – Tungamirai