Cmag Cmag - 1 year ago 110
Python Question

python mysql delete statement not working

here I am trying to remove any users which containt a " in their email/username.

def removeQuote(self, tbl,record):
""" Updates the record """
statmt="select id from %s WHERE `email` LIKE '%%\"%%'" % (tbl)
for idx, val in enumerate(rows):
id= val[0]
delstatmt = "DELETE FROM `maillist_subscription` WHERE id = '%s'" % id
print delstatmt

The output of this shows as if the action completed successfully, but the record remains in the database.
Output also shows a correct mysql statement:

DELETE FROM `maillist_subscription` WHERE id = '8288754'

Thanks for all your help!

Answer Source

You need to commit the change, using the commit() method on the connection object. Most DBAPI interfaces use implicit transactions.

Also, don't use string formatting for SQL query generation! It will open you up to SQL injections:


# What happens if id = "1'; DROP DATABASE somedb" ?
delstatmt = "DELETE FROM `maillist_subscription` WHERE id = '%s'" % (id,)


delstatmt = "DELETE FROM `maillist_subscription` WHERE id = ?"
cursor.execute(delstatmt, (id,))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download