python - Mysql fetchall方法抛出 "Unread result found"异常

标签 python mysql

我正在从 python 脚本中对 Mysql 执行查询。我正在使用 python 的 anaconda 发行版和 Oracle Mysql 模块。如果我将查询限制为 ~500 行,它将成功执行。高于此值的任何内容都会导致“找到未读结果”异常。我不明白我做错了什么。任何帮助表示赞赏。代码如下:

import mysql.connector
import csv

cnx = mysql.connector.connect(user='user', password='password',
                          host='server',
                          port='3306',
                          database='db',
                          connect_timeout=2000,
                          buffered=True)

try:

    query = "...large query that returns > 500 records..."

    cursor = cnx.cursor()
    cursor.execute(query)

    print 'this will print'
    results = cursor.fetchall()
    print 'this will not print'

    with open("data.csv", "wb") as csv_file:

        csv_writer = csv.writer(csv_file)
        csv_writer.writerows(results)

finally:
    cursor.close()
    cnx.close()

最佳答案

看起来你在缓冲内存中的行时内存不足,你的进程肯定被杀死了。尝试使用流式结果集而不是它(MySQLdb.cursors.SSCursor)。

cnx = MySQLdb.connector.connect(user='user', password='password',
                          host='server',
                          port='3306',
                          database='db',
                          connect_timeout=2000,
                          buffered=True)

try:

    query = "...large query that returns > 500 records..."
    cursor = MySQLdb.SSCursor(conn)
    #cursor = cnx.cursor()
    cursor.execute(query)
 ..

关于python - Mysql fetchall方法抛出 "Unread result found"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26541648/

相关文章:

python - 有没有办法匹配两个数据帧中的序列号,并将 df2 中的系列列表(来自行)添加到 df1 中的新列中(Python,pandas)

php - 来自 mysql 的 trader_sma 函数的数组

php - mysql 中的 num_row 操作?

utf-8 - 使用 UTF8 时控制台输出中的 mysql 表未对齐

python - 如果与另一个列表中的值匹配,则提取字符串

python - Bokeh - 在个人网站上呈现 Bokeh 图需要更高级别的步骤吗?

如果先前的选择成功,则 MySQL 引发错误不起作用

java - 错误连接为空

python - 将表从 google bigquery 导出到 google storage

python - Redshift COPY 操作在 SQLAlchemy 中不起作用