-1

I am trying to delete data from SQLite table where source column equals to "Online"

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reminders/com.reminders.FetchDataActivity}: android.database.sqlite.SQLiteException: no such column: Online (code 1): , while compiling: delete from ReminderTable where source = Online

Here is my code:

public void deleteOnlineReminders() {
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = " + "Online");
    }

Or Is it due to reason that I don't have any data into table where column source = Online

Sun
  • 6,258
  • 22
  • 69
  • 124

3 Answers3

1

You have to use 'Online' in your string concat (note the quotes).

As you can see from the exception, the compiled statement is otherwise wrong as you are refering to a column, not a string.

You may also want to use prepared statements instead of string concatenation for your parameter: How do I use prepared statements in SQlite in Android?

This is the more clean and safe version :)

Community
  • 1
  • 1
J. Dow
  • 553
  • 3
  • 13
  • I tried this way too : db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = Online"); but still getting same exception – Sun Apr 25 '16 at 10:03
  • 1
    db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'" Note the additional single Quotes around Online – J. Dow Apr 25 '16 at 10:22
1

Try this code.

db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + "=?",new String[] { "Online" });
Jay Rathod RJ
  • 10,321
  • 5
  • 27
  • 49
  • now getting : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reminders/com.reminders.FetchDataActivity}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: delete from ReminderTable where source = – Sun Apr 25 '16 at 10:17
  • 1
    @Sun I have just edited only added `Question Mark After Equal to` try and check. – Jay Rathod RJ Apr 25 '16 at 10:20
  • 1
    simply thank you... why we have used Question mark after equals to – Sun Apr 25 '16 at 10:26
  • @Sun It is used to pass field value which you have given in where condition. that's it. – Jay Rathod RJ Apr 25 '16 at 10:31
0
public void deleteOnlineReminders() {
    SQLiteDatabase db = this.getWritableDatabase();       
    dbHelper.delete(TABLE_REMINDERS, KEY_SOURCE + "=?", new String[] { Online})
}