python pymysql set autocommit false失败

时间:2012-02-11 16:43:32

标签: python mysql autocommit

我的脚本从文件读取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()

1 个答案:

答案 0 :(得分:4)

MySQL中的MyISAM存储引擎不支持事务。只有InnoDB支持ACID交易。

引用Transactions and Atomic Operations MySQL documentation

  

在交易方面,MyISAM表总是有效地运行   autocommit = 1模式。

你应该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