I’m trying to stop the user from entering the same string value for the “title” field in an appointment creation app in a calendar, as homework.
Here is what I thought of so far:
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;
}
But I don’t like the idea of having everything in my addAppointments method, I’d rather leave it clean and simple.
I tried doing the following as an alternative:
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();
}
}
but I get this error in both cases: 04-24 17:56:51.263: E/AndroidRuntime(17856): android.database.sqlite.SQLiteException: no such column: hello: , while compiling: SELECT title FROM appointments WHERE title = hello
Please if you have any advice please share, thank you.
You need to wrap
appointmentTitle.getText().toString()in single quotes:That way your assembled query looks like:
As other posters have courteously noted, however, this can cause an SQL injection issue if your query here takes User input. Adapting to a parameterized approach is the better way.