gpt4 book ai didi

python pymysql设置autocommit false失败

转载 作者:可可西里 更新时间:2023-11-01 08:07:21 25 4
gpt4 key购买 nike

我的脚本从文件中读取 MYSQL UPDATE 查询,然后应该使用 autocommit = 0 立即执行它们。但是,如果我删除 conn.commit() 它仍然会一个接一个地运行,尽管我没有 promise 。错误在哪里?

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支持交易。

关于python pymysql设置autocommit false失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com