使用 Python 的 MySQL 未读结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15336767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:51:10  来源:igfitidea点击:

MySQL Unread Result with Python

pythonmysql

提问by Gerenuk

I use mysql.connector to do SQL operations. I have a short scripts which executes the following operations (strings) on the cursor with cursor.execute(...):

我使用 mysql.connector 来做 SQL 操作。我有一个简短的脚本,它在光标上执行以下操作(字符串)cursor.execute(...)

"use {}".format(db)

"show tables"

command = """
ALTER TABLE Object DROP PRIMARY KEY;
ALTER TABLE Object ADD `id` bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;
ALTER TABLE Object ADD INDEX (`uid`);"""

The script iterates over several databases db.

该脚本遍历多个数据库db

The problem is that at some point I get an "Unread result found" error. It seems when I run the script, at some point "use mydb" returns a result (cursor._have_result=True), when I didn't expect one. The weird thing is that if I rerun the full script it runs a little longer with more databases giving the same error later.

问题是在某些时候我会收到“找到未读结果”错误。似乎当我运行脚本时,在某些时候“使用 mydb”会返回一个结果(cursor._have_result=True),而我没想到会返回一个结果。奇怪的是,如果我重新运行完整脚本,它会运行更长的时间,更多的数据库稍后会出现相同的错误。

Can you suggest a way to solve or investigate this problem? Is there something I can do to prevent "unread results"?

你能提出一种解决或调查这个问题的方法吗?我可以做些什么来防止“未读结果”?

PS: When I rerun the script the ALTER commands fails for the databases which are already done. Not sure if that causes problems.

PS:当我重新运行脚本时,对于已经完成的数据库,ALTER 命令失败。不确定这是否会导致问题。

采纳答案by geertjanvdk

Using MySQL Connector/Python, the Unread results foundmight happen when you use the connection object in different places without reading the result. It's not something one can go around. You can use the bufferedoptionto read result immediately.

使用 MySQL Connector/Python,当你在不同的地方使用连接对象而不读取结果时,可能会出现Unread results found。这不是一个人可以绕过的东西。您可以使用缓冲选项立即读取结果。

As mentioned in the comments, it's best to split the statements and execute them separately.

正如评论中提到的,最好将语句拆分并单独执行。

If you want to execute multiple statements, you'll need to use the multi=Trueoption for the MySQLCursor.execute()method (since Connector/Python v1.0.4). Actually, if you don't use the multioption and send multiple statements, an InterfaceError will raise. (I do suspect a bug here as well..)

如果要执行多个语句,则需要对MySQLCursor.execute()方法使用multi=True选项(自连接器/Python v1.0.4 起)。实际上,如果您不使用multi选项并发送多个语句,则会引发 InterfaceError 。(我也怀疑这里有一个错误..)

Additional remarks:

补充说明:

  • Instead of executing the USE-command to change databases, you can MySQLConnection.database property.
  • You best group the changes into one ALTER TABLE statement, like this:

    ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT NOT NULL AUTO_INCREMENT KEY FIRST, ADD INDEX(c1)

  • 您可以使用MySQLConnection.database 属性,而不是执行 USE 命令来更改数据库。
  • 您最好将更改组合到一个 ALTER TABLE 语句中,如下所示:

    ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT NOT NULL AUTO_INCREMENT KEY FIRST, ADD INDEX(c1)

回答by Shahzaib Chadhar

You have to pass buffered = truein your cursor. Read more official docs

你必须传入buffered = true你的光标。阅读更多官方文档

cursor = conn.cursor(buffered=True)