python pymysql set autocommit false fails

陌路散爱 提交于 2019-12-22 10:38:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!