1

当我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;
...

其他查询中也有类似的情况。有人知道问题出在哪里吗?

4

1 回答 1

6

您必须?为每个值使用一个...

String selection = DBColumns.History._ID + " in (?, ?, ?)";
String selectionArgs[] = new String[]{"1","2","3"};

或(来自IN 子句和占位符中的一个想法)

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 (?, ?, ?)"'

对不起我的英语

我们都说安卓。:)

于 2012-12-29T18:45:20.243 回答