2

Hi can I do this??,

int[] ids=new int[]{1,2,3,4,6,7,8,9,10};

db.execSQL(DELETE FROM TABLE_NAME WHERE id NOT IN ?,ids);

basically I want to delete all the rows that doesn't match my ids array.

7
  • SO where is the problem? ids.toString will return object name Commented Mar 20, 2013 at 18:41
  • ids.toString become [1,2,3,4,5,6,7,8,9,10] and de array should be something like this (1,2,3,4,5,6,7,8,9,10) Commented Mar 20, 2013 at 18:42
  • 1
    I believe you answered your own question there... Commented Mar 20, 2013 at 18:44
  • you are rigth,to string give the object name. can I convert ids into (1,2,3,4,5,6,7,8,8,10) to use in a sqlite query like that one? Commented Mar 20, 2013 at 18:52
  • Ahh sorry - as @Smit mentioned: calling toString() on an array will return the instance name of the array. If you were to use an ArrayList instead you'd get [1,2,3,4,5...] returned. Anyways, using brackets in the SQL statement will fail. Commented Mar 20, 2013 at 18:53

2 Answers 2

3

So your problem is that you placed only one placeholder which will be replaced only with one value and array of args should have only one item. It should potentially work if you had 9x ? for each item in array.

It works as one placeholder - one value (same order). I wrote for you this snippet of code. Check it out.

int[] ids = new int[] {1,2,3,4,6,7,8,9,10};
StringBuilder query = new StringBuilder();
query.append("delete from table where column_id not in (");
for (int i = 0; i < ids.length; i++) {
    query.append("?");
    if (i < ids.length - 1) {
        query.append(",");
    }
}
query.append(")");
db.execSQL(query.toString(), ids);

Now it should works. Also ? character in dynamically created query is called placeholder and each placeholder will be replaced with value from String[] in same order.

I pretty recommend an usage of placeholders, whenever you will work with database. This approach is cleaner, safer and better looking.

Update for API 11+

public int delete() {
    int[] ids = new int[] {1,2,3,4,6,7,8,9,10};
    String[] args = new String[ids.length];
    StringBuilder query = new StringBuilder();
    query.append("delete from table where column_id not in (");
    SQLiteStatement stmt = null;
    for (int i = 0; i < ids.length; i++) {
        args[i] = String.valueOf(ids[i]);
        query.append("?");
        if (i < ids.length - 1) {
            query.append(",");
        }
    }
    query.append(")");
    stmt = db.compileStatement(query.toString());
    stmt.bindAllArgsAsStrings(args);
    return stmt.executeUpdateDelete();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Smit executeUpdateDelete(); is only from API 11.
Oh I got it its for Android sqlLite.
1

You can use:

java.util.Arrays.toString(ids).replace('[', '(').replace(']', ')')

to convert the array to a suitable SQL where clause format

1 Comment

would this work for an arrayList of strings? or does something need to be modified?

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.