I'm using MySQLdb in Python 2, and I have a question about executing queries into the database. Say I have some connection con
, and I instantiate a cursor with cur = con.cursor()
. Which of the following is the proper way to commit changes to the database? Bonus points if you could explain the theory behind the correct answer :)
Method 1:
try:
cur.execute('command 1')
con.commit()
cur.execute('command 2')
con.commit()
except MySQLdb.Error as e:
con.rollback()
Method 2:
try:
cur.execute('command 1')
cur.execute('command 2')
con.commit()
except MySQLdb.Error as e:
con.rollback()
Method 3:
try:
cur.execute('command 1')
try:
cur.execute('command 2')
except MySQLdb.Error as e:
con.rollback()
con.commit()
except MySQLdb.Error as e:
con.rollback()