当我在查询中使用selectionArgs
时,应用程序崩溃了
logcat的
12-30 22:22:05.406: ERROR/AndroidRuntime(532): FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: DELETE FROM _history WHERE _id in ?
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1741)
at ru.sofron.money.db.DataProvider.delete(DataProvider.java:201)
at android.content.ContentProvider$Transport.delete(ContentProvider.java:213)
at android.content.ContentResolver.delete(ContentResolver.java:822)
at ru.sofron.money.activities.HistoryFragment$1.onActionItemClicked(HistoryFragment.java:126)
at android.widget.AbsListView$MultiChoiceModeWrapper.onActionItemClicked(AbsListView.java:5648)
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:2473)
at com.android.internal.app.ActionBarImpl$ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:757)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:490)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:108)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
使用
String selection = DBColumns.History._ID + " in ?";
String selectionArgs[] = new String[1];
selectionArgs[0] = Arrays.toString(mListView.getCheckedItemIds());
getActivity().getContentResolver()
.delete(DataProvider.HISTORY_URI,
selection,
selectionArgs);
在内容提供商中删除
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = uriMatcher.match(uri);
int rowsDeleted;
switch (uriType) {
case HISTORY:{
rowsDeleted = mDb.delete(DBColumns.History.TABLE_NAME, selection,selectionArgs);
}break;
...
类似的情况在其他查询中。 有谁知道问题出在哪里?
答案 0 :(得分:6)
您必须为每个值使用一个?
...
String selection = DBColumns.History._ID + " in (?, ?, ?)";
String selectionArgs[] = new String[]{"1","2","3"};
或(来自IN clause and placeholders中的想法)
String selectionArgs[] = new String[]{"1","2","3"};
String selection = DBColumns.History._ID + " in (";
for(int i = 0; i < selectionArgs.length; i++)
selection += "?, ";
selection = selection.substring(0, selection.length() - 2) + ")";
// selection is 'DBColumns.History._ID + " in (?, ?, ?)"'
抱歉我的英文
我们都说Android。 :)