在Android SQLITE中删除多行

时间:2019-04-14 14:28:17

标签: android database sqlite android-sqlite

在我的应用程序中,我试图同时执行两个删除查询,这些查询使用相同的输入删除。我正在使用IN子句。但是我遇到了错误。我的键是一个字符串值。

方法。...

public void deleteUsingList(String[] ids) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_FAV_AMEN, KEY_AD_ID + " IN (" + "?)", ids);
    db.delete(TABLE_FAV_HEAD, KEY_AD_ID + " IN (" + "?)", ids);
    db.close();
}

我的错误。...

2019-04-14 19:57:48.102 14802-14802/com.estate.dushanmadushanka.estate E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.estate.dushanmadushanka.estate, PID: 14802
    java.lang.IllegalArgumentException: Too many bind arguments.  2 arguments were provided but the statement needs 1 arguments.
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java)
        at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java)
        at com.estate.dushanmadushanka.estate.other.SQLiteHandler.deleteUsingList(SQLiteHandler.java:159)
        at com.estate.dushanmadushanka.estate.activity.FavouriteActivity$2.onClick(FavouriteActivity.java:84)
        at android.view.View.performClick(View.java)
        at android.view.View$PerformClick.run(View.java)
        at android.os.Handler.handleCallback(Handler.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

2 个答案:

答案 0 :(得分:2)

        passage1Button.Clicked += (sender, args) =>
         {
            var contentpage = new ShowTextPage("Passage text here, goes to page constructor where label is set to passed text");

            var page = new NavigationPage(contentpage);

            Navigation.PushModalAsync(page);
         }

答案 1 :(得分:1)

您不能像这样将参数传递给db.delete(TABLE_NAME, "CAST("+KEY_ID+" AS TEXT) IN (" + new String(new char[ids.length-1]).replace("\0", "?,") + "?)", ids); 子句。
我想IN是要删除的ID的ids数组。
您必须创建一个由String分隔的数组ids的所有项目组成的字符串:

,

您将需要此导入:

String strIn = TextUtils.join(",", ids);

然后:

import android.text.TextUtils;

如果id是整数,这将起作用,但是如果它们是字符串,则必须先将它们全部用单引号引起来。