1

I'm using MySQLdb in Python3 to execute some queries. One of which I have a list of user_ids and want to delete from my user table based off of them.

When I execute the query I get a warning from MySQLdb:

Warning: (1292, "Truncated incorrect DOUBLE value: '108, 114, 109, 115'")

The query attempted to be executed is:

DELETE FROM user
WHERE id IN (108, 114, 109, 115);

My code looks like the following:

cur.execute(get_users)
users = cur.fetchall()
users = [item[0] for item in users]
users = ", ".join(str(user) for user in users)

cur.execute(delete_query, [users])

I have also tried to do the following, it results in no warning however checking my database I can still see those users existing:

users = ", ".join("\'{}\'".format(user) for user in users)

The following works perfectly, however isn't secure: cur.execute(delete_query.format(users))

And yes, I am executing a db.commit() in my code.

1 Answer 1

1

The following approach has worked for me. The users list will be going directly to the MySQL driver so injection is not an issue:

format_strings = ','.join(['%s'] * len(users))
cursor.execute("DELETE FROM user WHERE id IN (%s)" % format_strings,
                tuple(users))
Sign up to request clarification or add additional context in comments.

1 Comment

Just found this from the thread: stackoverflow.com/a/589416/1327636 It seems to work for me as well, thanks!

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.