【问题标题】:Python MySQL many function show InternalError: Unread result foundPython MySQL 多函数显示 InternalError: Unread result found
【发布时间】:2020-04-02 09:06:15
【问题描述】:

我创建函数 function1_db() 用于从列 j_id 获取最新的 id,而 function2_read() 用于插入新数据。每个函数都必须像这样连接到 MySQL 数据库。

mydb = mysql.connector.connect(
  host="xxxxxxxxxxx",
  user="xxx",
  passwd="xxxxxx",
  database="test_db"
)


def function1_db():

    last_id = 0 

    mycursor = mydb.cursor()

    sql = "SELECT * FROM log ORDER BY j_id DESC"

    mycursor.execute(sql)
    result = mycursor.fetchone()

    mycursor.close()

    if result != None:

        last_id = result[4]

    return last_id

def function2_read(last_id):

    mycursor = mydb.cursor()

    res = "xxx"
    if not res.empty:

        sql = "INSERT INTO log (id, user, number, time, j_id, alert) VALUES (%s, %s, %s, %s)"
        val = [(None, res['User'], res['Pages'] , res['State'], last_id, 0 )]

        mycursor.executemany(sql, val)

        mydb.commit()

        print(mycursor.rowcount, "was inserted.") 

        mycursor.close()

当我运行此代码时,它会显示这样的错误。如何解决?

    raise errors.InternalError("Unread result found")

InternalError: Unread result found

【问题讨论】:

    标签: python database mysql-python


    【解决方案1】:

    尝试设置 Buffered = true (from related question)

    mycursor = mydb.cursor(buffered=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多