我試圖阻止用戶輸入日曆中預約創建應用中的「標題」字段的相同字符串值作爲作業。是否可以在沒有遊標的情況下檢查sqlite值? - Android
這是我認爲的迄今:
private static String[] CHECK = {TITLE};
private Cursor addAppointment(String title, String time, String details){
calendarData = new CalendarData(this);
SQLiteDatabase db1 = calendarData.getReadableDatabase();
SQLiteDatabase db = calendarData.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE, calendar.getDate());
values.put(TITLE, title);
values.put(TIME, time);
values.put(DETAILS, details);
db.insertOrThrow(TABLE_NAME, null, values);
Cursor titleCursor = db1.query(TABLE_NAME, CHECK, TITLE+" = "+appointmentTitle.getText().toString(), null, null, null, null);
if(titleCursor.getString(0) != null){//MEANING THERE IS A DUPLICATE
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("You've entered a duplicate title field, please rename.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
alertDialog.show();
}
return titleCursor;
}
,但我不喜歡在我的addAppointments方法一切的想法,我寧願把它乾淨和簡單。
我試着做以下作爲替代:
private static String[] CHECK = {TITLE};
private void addAppointment(String title, String time, String details){
calendarData = new CalendarData(this);
SQLiteDatabase db1 = calendarData.getReadableDatabase();
SQLiteDatabase db = calendarData.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE, calendar.getDate());
values.put(TITLE, title);
values.put(TIME, time);
values.put(DETAILS, details);
db.insertOrThrow(TABLE_NAME, null, values);
}
private Cursor checkTitle(){
calendarData = new CalendarData(this);
SQLiteDatabase db1 = calendarData.getReadableDatabase();
Cursor titleCursor = db1.query(TABLE_NAME, CHECK, TITLE+" = "+appointmentTitle.getText().toString(), null, null, null, null);
startManagingCursor(titleCursor);
return titleCursor;
}
private void showTitleError(Cursor cursor){
if(cursor.getString(0) != null){//MEANING THERE IS A DUPLICATE
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("You've entered a duplicate title field, please rename.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
alertDialog.show();
}
}
,但我在這兩種情況下出現此錯誤:04-24 17:56:51.263:E/AndroidRuntime(17856):android.database.sqlite .SQLiteException:沒有這樣的列:你好:,在編譯時:SELECT標題FROM約會WHERE標題=你好
請如果你有什麼建議請分享,謝謝。
*請仔細閱讀*錯誤信息。請注意它如何與標題無關。 -1(缺乏問題/問題細化和嚴重錯誤的標題)和「太本地化」。 – 2012-04-24 18:09:29