Python MySql连接器准备的语句和字典类
如何使游标成为一个准备好的语句和一个字典。 我有以下几点Python MySql连接器准备的语句和字典类,python,mysql,python-3.x,mysql-connector,Python,Mysql,Python 3.x,Mysql Connector,如何使游标成为一个准备好的语句和一个字典。 我有以下几点 cursor(cursor_class = MySQLCursorPrepared) prints <class 'mysql.connector.cursor.MySQLCursorPrepared'> cursor(dictionary = True) prints <class 'mysql.connector.cursor.MySQLCursorDict'> 这是错误的 Cursor not av
cursor(cursor_class = MySQLCursorPrepared)
prints <class 'mysql.connector.cursor.MySQLCursorPrepared'>
cursor(dictionary = True)
prints <class 'mysql.connector.cursor.MySQLCursorDict'>
这是错误的
Cursor not available with given criteria: dictionary, prepared
有趣的是
cursor(cursor_class = MySQLCursorPrepared,dictionary = True)
我没有得到错误,但数据不是字典类型 我也有同样的问题。准备好的和字典不在一起 我报告了错误:
现在我们等待;) 这种简单的解决方法似乎对我有效-如果我禁用SSL,我可以在“strace”输出中看到,多个相同的查询只发送到服务器一次,当我迭代光标的行时,我肯定会返回字典:
from mysql.connector.cursor import MySQLCursorDict, MySQLCursorPrepared
class PreparedDictionaryCursor(MySQLCursorDict, MySQLCursorPrepared):
pass
压缩
cursor.column\u names
和行值是一个很好的解决方法:
cursor = connection.cursor(prepared=True)
query = 'SELECT foo, bar FROM some_table WHERE a = %s AND b = %s'
a = 1
b = 42
cursor.execute(query, [a, b])
rows = cursor.fetchall()
for row in rows:
result = dict(zip(cursor.column_names, row))
print(result['foo'], result['bar'])
有关MySQLCursor.column\u names的文档,请参阅。我找到了一个解决办法。我把它做成一个预先准备好的语句,然后用dictzip把数据输入字典。看来他们不会解决这个问题。我希望他们也能解决类似的声明问题。目前还没有一种干净的方法可以使用准备好的语句来执行like语句<代码>从表格中选择*列,如%s我认为您的最后一行应该是打印的(结果['foo'],结果['bar']),谢谢,@KenIngram。固定的。
cursor = connection.cursor(prepared=True)
query = 'SELECT foo, bar FROM some_table WHERE a = %s AND b = %s'
a = 1
b = 42
cursor.execute(query, [a, b])
rows = cursor.fetchall()
for row in rows:
result = dict(zip(cursor.column_names, row))
print(result['foo'], result['bar'])