1

I need to delete multiple rows from my database table. This is my code for deletion

String[] paragraph;
.....
//here you can to put various strings within it (1 or more)
.....
db.delete(mytablename,"Paragraph = ?",paragraphs);

It works with 1 string, but it doesn't work with more strings. Can someone help me, please?

Here there is the error shown by LogCat:

05-11 11:03:01.400: E/AndroidRuntime(767): FATAL EXCEPTION: main
05-11 11:03:01.400: E/AndroidRuntime(767): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x377500
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:241)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.database.DatabaseUtils.bindObjectToProgram(DatabaseUtils.java:191)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1769)
05-11 11:03:01.400: E/AndroidRuntime(767):  at it.tirocinio.Segnalibro$3$3.onClick(Segnalibro.java:196)
05-11 11:03:01.400: E/AndroidRuntime(767):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:123)
05-11 11:03:01.400: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-11 11:03:01.400: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-11 11:03:01.400: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:521)
05-11 11:03:01.400: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-11 11:03:01.400: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-11 11:03:01.400: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
4
  • 2
    you need an equal number of ?:s as you have entries in your paragraph array, e.g. "Paragraph=? OR Paragraph=? OR Paragraph=?" for an array with a length of 3. Commented May 11, 2012 at 9:56
  • 1
    It wont work for more than one string. Paragraph = ? in db.delete will accept only one selection argument. you can use something like db.rawquery(DELETE FROM table WHERE columnName IN ('name1', 'name2', 'name3')); Commented May 11, 2012 at 10:05
  • I understand the reason, I can't use the your solutions because the number of strings is variable. I think that I must use a for and repeat the delete statement. Any advice? I would like to have the best solution. Commented May 11, 2012 at 10:21
  • @sleone08 I dont think for loop for is a good approach because that would be too costly to have one query each for deletion of a row. Commented May 11, 2017 at 11:20

1 Answer 1

7
use this way:


for(int i = 0 ; i < paragraphs.lenght;i++)
{

db.delete(mytablename,"Paragraph ='"+paragraphs[i]+"'",null);
}
Sign up to request clarification or add additional context in comments.

2 Comments

perfect solution.. :)
Isn't there a way to do so in a single query? this approach would be too costly for (say) some 100 rows.

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.