在Android SQLITE中删除多行

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述在Android SQLITE中删除多行相关的知识,希望能为你提供帮助。
在我的应用程序中,我试图同时执行两个删除查询,使用相同的输入删除。我正在使用IN子句。但是我收到了一个错误。我的键是一个String值。
方法....

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(); }

【在Android SQLITE中删除多行】我的错误......
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)

答案
db.delete(TABLE_NAME, "CAST("+KEY_ID+" AS TEXT) IN (" + new String(new char[ids.length-1]).replace("", "?,") + "?)", ids);

另一答案你不能像这样将参数传递给IN子句。 我想ids是你要删除的id的String数组。 您必须创建一个字符串,其中包含使用ids分隔的数组,的所有项:
String strIn = TextUtils.join(",", ids);

你需要这个导入:
import android.text.TextUtils;

然后:
db.delete(TABLE_FAV_AMEN, KEY_AD_ID + " IN (?)", strIn);

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

    推荐阅读