Search code examples
androiddatabasesqliterecords

Query for deleting a particular record form the SQLite Database in android


I want to delete the selected record form my SQLite Database in my android application. But I can't do it and getting the exception while program runs.

Here is what I am trying to delete the Particular Selected Record.

 public void deleteRecord(String datarecord)
   {
       this.db.delete(TABLE_NAME, "name="+datarecord , null);
   }

And this is what I am doing on the click event from where I perform the delete operation of the selected Record.

dh.deleteRecord(FavMusicList[position]);

dh is the object of my DataHelper Class where I have created the deleteRecord function.

I am getting the SQLite Exception for the Syntax Error. So I guess I am surely making mistake with the Syntax to delete the Particular Selected Record but couldn't know what exactly I have to correct.

Can anybody please help me in this?

Thanks, david

My Stack Trace:

12-22 14:39:37.892: ERROR/AndroidRuntime(676): FATAL EXCEPTION: main
12-22 14:39:37.892: ERROR/AndroidRuntime(676): android.database.sqlite.SQLiteException: near "very": syntax error: , while compiling: DELETE FROM table1 WHERE name=Iâm very drunk
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1589)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at com.android.soundmachine.DataHelper.deleteRecord(DataHelper.java:42)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at com.android.soundmachine.FavoritesListing$SoundMacHineAdapter$1$1.onClick(FavoritesListing.java:192)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.os.Looper.loop(Looper.java:123)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at java.lang.reflect.Method.invoke(Method.java:521)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-22 14:39:37.892: ERROR/AndroidRuntime(676):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • DELETE FROM table1 WHERE name=Iâm very drunk

    Space is not allowed. You should give quote, or double quote in the WHERE clause.