问题
my script reads MYSQL UPDATE queries from a file and then should execute them at once by using autocommit = 0. However, if I remove conn.commit()
it still runs one by one, although I have not commited. Where is the error?
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()
回答1:
Transactions are not supported with MyISAM storage engine in MySQL. Only InnoDB suports ACID transactions.
Quoting Transactions and Atomic Operations MySQL documentation:
In transactional terms, MyISAM tables effectively always operate in autocommit = 1 mode.
You should 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)
Move tables to a MySQL storage engine that supports transactions.
来源:https://stackoverflow.com/questions/9241952/python-pymysql-set-autocommit-false-fails