我试图阻止用户在日历中的约会创建应用程序中为“标题”字段输入相同的字符串值,作为家庭作业。
到目前为止,这是我的想法:
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: no such column: hello: , while compile: SELECT title FROMointments WHERE title =你好
如果有什么建议请分享,谢谢。