我们正在尝试将包含多个插入语句的SQL文件作为一个查询运行,但是rollback当任何一个语句包含错误时,它似乎都会失败。
MySQLd配置:
sql_mode = STRICT_ALL_TABLES
default-storage-engine = innodb
Python代码:
from contextlib import closing
import MySQLdb
database_connection = MySQLdb.connect(host="127.0.0.1", user="root")
with closing(database_connection.cursor()) as cursor:
database_connection.begin()
cursor.execute('DROP DATABASE IF EXISTS db_name')
cursor.execute('CREATE DATABASE db_name')
cursor.execute('USE db_name')
cursor.execute('CREATE TABLE table_name(first_field INTEGER)')
with closing(database_connection.cursor()) as cursor:
try:
database_connection.begin()
cursor.execute('USE db_name')
cursor.execute('INSERT INTO table_name VALUES (1)'