AndroidRuntime android.database.SQLiteException:near“=”:语法错误

时间:2013-07-11 16:48:46

标签: android android-contentprovider

现在有这个问题了。我不知道为什么我会收到这个错误。我试图从数据库中删除一行。我没有插入或更新的问题,它的删除功能。

这是我的日志:

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)

这是我的删除功能:

    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;
}

这就是我如何称呼它

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

正如你在日志的第二行看到的那样,我有一个带有标签“MyTag”的用户日志,这实际上是应该删除的行号,它在这里看起来不错,但我不知道为什么我会得到错误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=

我看到错误是没有传递行号但是我确实传入了行号并且也正确记录了它。不能成为uri匹配器,因为update完美无缺。

2 个答案:

答案 0 :(得分:3)

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

你忘记了吗?。

答案 1 :(得分:2)

类似问题: 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)});