nupac
nupac

Reputation: 2489

AndroidRuntime android.database.SQLiteException: near "=": syntax error

Have had this problem for a while now. I dont know why I get this error. I am trying to delete a row from the database. I have no problems with insertion or update, its jus the delete function.

Here's my log:

07-11 16:32:06.296: W/CursorWrapperInner(1653): Cursor finalized without prior close()
07-11 16:32:07.706: V/MyTag(1653): 2
07-11 16:32:07.706: E/SQLiteLog(1653): (1) near "=": syntax error
07-11 16:32:07.706: D/AndroidRuntime(1653): Shutting down VM
07-11 16:32:07.706: W/dalvikvm(1653): threadid=1: thread exiting with uncaught exception  (group=0x40a71930)
07-11 16:32:07.737: E/AndroidRuntime(1653): FATAL EXCEPTION: main
07-11 16:32:07.737: E/AndroidRuntime(1653): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: DELETE FROM travelInfoTb WHERE _id=
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1491)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at org.classapp.traveldiary.TravelInfoProvider.delete(TravelInfoProvider.java:67)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.content.ContentProvider$Transport.delete(ContentProvider.java:228)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.content.ContentResolver.delete(ContentResolver.java:958)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at org.classapp.traveldiary.DiaryEditActivity$2.onClick(DiaryEditActivity.java:125)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.os.Looper.loop(Looper.java:137)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at java.lang.reflect.Method.invoke(Method.java:511)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-11 16:32:07.737: E/AndroidRuntime(1653):     at dalvik.system.NativeStart.main(Native Method)

Here is my delete function:

    public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    Log.v("MyTag", selectionArgs[0]);
    int rows_deleted = db.delete(TRAVELINFO_TABLE_NAME, selection, selectionArgs); 
    Log.i("rows_deleted", String.valueOf(rows_deleted));
    return rows_deleted;
}

and here is how I call it

ContentResolver cr = getContentResolver();
            int rows = cr.delete(TravelInfoTb.CONTENT_URI, TravelInfoColumns._ID + "=" , new String[]{Long.toString(row_id)});

As you can see on the second line of the log I have a user log with the tag "MyTag" which is actually the row number that is supposed to be deleted and it looks alright here but I have no idea why I get an error FATAL EXCEPTION: main 07-11 16:32:07.737: E/AndroidRuntime(1653): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: DELETE FROM travelInfoTb WHERE _id=

I see the error is that no row number is being passed but I did pass in the row number and logged it correctly too. Cannot be the uri matcher because update works perfectly.

Upvotes: 2

Views: 908

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

Similar Question : Delete row in database table given one column value - which is a string

int rows = cr.delete(TravelInfoTb.CONTENT_URI, TravelInfoColumns._ID + "= ?" , new String[]{Long.toString(row_id)});

Upvotes: 2

Ziem
Ziem

Reputation: 6697

int rows = cr.delete(TravelInfoTb.CONTENT_URI, TravelInfoColumns._ID + "= ?" , new String[]{Long.toString(row_id)});

You forgot about ?.

Upvotes: 3

Related Questions