2

In my app, I am trying to execute two delete queries at the same time, which delete using same input. I am using IN clause. But I am getting an error. My key is a String value.

Method....

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

My error....

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 Answers 2

2
db.delete(TABLE_NAME, "CAST("+KEY_ID+" AS TEXT) IN (" + new String(new char[ids.length-1]).replace("\0", "?,") + "?)", ids);
Sign up to request clarification or add additional context in comments.

Comments

0

You can't pass parameters to the IN clause like this.
I suppose ids is a String array of the ids you want to delete.
You must create a string consisting of all the items of the array ids delimited with ,:

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

you will need this import:

import android.text.TextUtils;

and then:

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

This will work if the ids are integers, but if they are strings you have to enclose them all in single quotes first.

1 Comment

ids are Strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.