我的脚本从文件读取MYSQL UPDATE查询,然后应该使用autocommit = 0立即执行它们.但是,如果我删除conn.commit(),它仍然会逐个运行,尽管我尚未提交.错误在哪里?
import pymysql
conn = pymysql.connect(host='x', unix_socket='/tmp/mysql.sock',user='x', passwd='x', db='x')
fileHandle = open ( 'mysqlout.txt' )
fileList = fileHandle.readlines()
fileHandle.close()
i = 1
weiter = input("Execute MYSQL file? ")
if (weiter == 'y'):
cur = conn.cursor()
cur.execute('SET autocommit = 0')
conn.commit()
for fileLine in fileList: #-----each line is an UPDATE...query
cur.execute(fileLine)
i = i + 1
print(i," ---",round(i / len(fileList),3))
#conn.commit()
conn.close()
解决方法:
MySQL中的MyISAM存储引擎不支持事务.只有InnoDB支持ACID事务.
引用Transactions and Atomic Operations MySQL documentation:
In transactional terms, MyISAM tables effectively always operate in
autocommit = 1 mode.
您应该check storage engine for your tables:
mysql> SELECT table_name,engine FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema=DATABASE();
+--------------+--------+
| table_name | engine |
+--------------+--------+
| stats | MyISAM |
+--------------+--------+
1 row in set (0.00 sec)
将表移动到支持事务的MySQL storage engine.