这个问题已经有一段时间了。我不知道为什么我会收到这个错误。我正在尝试从数据库中删除一行。我对插入或更新没有问题,它只是删除功能。
这是我的日志:
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
效果很好。