我尝试将准备好的语句的结果直接提取到字典中,但出现错误:
ValueError: Cursor not available with given criteria: buffered, dictionary, prepared
我读到这是由于默认情况下游标没有被缓冲,但我启用了缓冲,结果没有变化。
完整代码如下。
import mysql.connector
def __init__(self):
self.mydb= mysql.connector.connect(
host="host",
user="user",
passwd="password",
database="db",
buffered=True
)
self.cur = self.mydb.cursor(prepared=True, dictionary=True)
self.fetch_permit = """SELECT PERMIT_ID,BUILDER_BUSINESS_NAME, BUILDER_ZIP_CODE, BUILDER_PHONE_NUMBER
FROM MASTER_TABLE WHERE permit_id = %s """
def getPermits(self,permit_id):
permits = self.cur.executemany(self.fetch_permit, permit_id)
return permits
info.getPermits(permit_id='1000') # throws error
最佳答案
可能不是最好的解决方案,但我能够通过更改我正在使用的 mySQL 库来解决问题:
导入pymysql
然后我改变了光标:
self.mydb.cursor(pymysql.cursors.DictCursor)
现在按预期工作。
关于Python MySQL 字典准备语句抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756368/