Skip to content Skip to sidebar Skip to footer

Python Mysqldb Delete Row

for some reason, this is bombing out: print tbl,record statmt='DELETE FROM '%s' WHERE email LIKE '%s'' %(tbl,record) print statmt self.cursor.execute(statmt) error: maillist_frogs

Solution 1:

I was having a similar problem just now.

After further research, I realized I'd forgotten to do a connection.commit() after the delete, or, as I found somewhere else, you could just do cursor.execute("set autocommit = 1") before doing any other data operations to have them automatically committed right away, if you don't need control over transactions commits.

This could have been the trouble with Cmag's code, though without seeing more, it's hard to tell.

Solution 2:

defremoveRow(self,tbl,record):
        """ Remove specific record """
        record = record[0]
        statmt="DELETE FROM %s WHERE email LIKE '%s'" %(tbl,record)
        self.cursor.execute(statmt)

Solution 3:

You have two issues:

The first one, that is causing your crash, is that you quoted your table name, using regular quotes. If you want to quote table names, you should use backquotes (`).

Second issue, you're 'Not doing it right'. You should not create your queries using string formatting, but instead let Python MySQLdb's cursor.execute do it for you, properly.

To do so, try the following:

statmt = "DELETE FROM %s WHERE email LIKE %s"self.cursor.execute(statmt, (tbl, record))

By the way, using string formatting on MySQLdb queries exposes you to SQL injection in case you're going to use your application on the Web or something. You should not be using string formatting to create queries.

Post a Comment for "Python Mysqldb Delete Row"