1

I have a python script that connects to a local MySQL db. I know it is connecting correctly because I can do this and get the proper results:

cursor.execute("SELECT * FROM reel")

But when I try to do any insert statements it just does nothing. No error messages, no exceptions. Nothing shows up in the database when I check it from sqlyog. This is what my code looks like:

self.cursor.executemany("INSERT INTO reel (etime,etext) VALUES (%s,%s)", tups)

where tups is a list of tuples looking like this ('0000-00-00 00:00:00','text'). No errors show up and if I copy paste the generated SQL query into sqlyog it works. I've tried generating the query and doing cursor.execute() on it and no errors and no result either. Anyone know what I'm doing wrong?

1 Answer 1

6

You need to do a self.cursor.commit() after self.cursor.executemany("INSERT INTO reel (etime,etext) VALUES (%s,%s)", tups)

Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.

Conversely, you can also use connection.rollback() to throw away any changes you've made since the last commit.

Important note: Some SQL statements -- specifically DDL statements like CREATE TABLE -- are non-transactional, so they can't be rolled back, and they cause pending transactions to commit.

Is a FAQ

2
  • thanks... that was it. i read like 4 different guides on python mysql and i never noticed the part that said to do commit lol.
    – Barakat
    Aug 9, 2010 at 17:08
  • Yeah, I ran into a problem where on MySQL 5.1.3 and earlier, I didn't need to add the commit statement, but on MySQL 5.1.42 and later I had to add conn.commit() after my execute statements. Aug 9, 2010 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.