6

If list LL:

LL = ['foo', bar', 'noo', 'boo',]

is in a MySQL table, test in column ID with other ID's.

I could use the following to delete all rows with ID's in LL:

 csr.execute("""DELETE FROM test.test WHERE ID = "Foo"; """)
  csr.execute("""DELETE FROM test.test WHERE ID = "bar"; """)  
  csr.execute("""DELETE FROM test.test WHERE ID = "noo"; """)
  csr.execute("""DELETE FROM test.test WHERE ID = "boo"; """)  

How could I do it programatically?

6 Answers 6

12

You can do it with a single query:

id_list = ['abc', 'def', 'ghi']
query_string = "delete from test where id in (%s)" % ','.join(['?'] * len(id_list))
cursor.execute(query_string, id_list)

Since cursor.execute escapes strings when doing substitutions, this example is safe against SQL injections.

Sign up to request clarification or add additional context in comments.

9 Comments

No, see how the query string in the answer says "where id in ..." not "where id = ..."
@user428862 "in ('abc', 'hjk', ... )" works in MySQL. It's preferred over "where id='abc' or id='hjk'". However my example contained a syntax error and numerical IDs. Try the updated version.
id_list = ['acb','def', ] The SQL contains 0 parameter markers, but 2 parameters were supplied'
You need to construct the query_string variable as specified. It replaces each id with a %s.
print of query_str " delete from test where id in (%s,%s)
|
2

For MySQL you need to use %s instead of ? as the parameter marker. And don't forget to commit.

product_list = [645, 64, 9785, 587]
query = "DELETE FROM products WHERE id IN (%s)" % ",".join(["%s"] * len(product_list))
cursor.execute(query, product_list)
connection.commit()

Comments

1

String formatters - http://docs.python.org/library/string.html#format-string-syntax

["""DELETE FROM test.test WHERE ID = "%s"; """ % x for x in LL]

and then run each of the SQL statements in the list.

1 Comment

It is of critical importance that LL contains only trusted items. If it contains data from untrusted sources and not properly escaped, SQL injection is possible.
1
for item in LL:
    csr.execute("DELETE FROM test.test WHERE ID = '%s'", item)

like that?

2 Comments

typo- each should be item. fixed.
that bad example , u are executing many queries which lead to overloading your db
0

Just convert the list into a string format with comma-separated and use a normal where clause with in condition.

id_list = ['abc', 'def', 'ghi']
id_list_string = "', '".join(id_list)
delete_query = "delete from test where id in ('" +id_list_string+"')"
dbconnection.execute(delete_query)

1 Comment

It would be helpful if you explain your answer.
-2

We can use one delete query and convert the list into tuple.

list_ids = ['foo', bar', 'noo', 'boo',]
delete_query = "DELETE FROM test.test WHERE ID=%s"
delete_records = tuple(list_ids)
cursor.executemany(delete_exec, delete_records)

Comments

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.