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

?値ごとに1 つ使用する必要があります...

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

ごめんなさい、私の英語

私たちは皆 Android を話します。:)

于 2012-12-29T18:45:20.243 に答える