我正在使用flask框架和mysql数据库连接器。
我做了以下工作:
from flaskext.mysql import MySQL
mysql = MySQL()
## app.config database configuration code
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor();
cursor.execute(
'SELECT * FROM customer WHERE email = (%s)', (username,)
)
user = cursor.fetchone()
我可以使用索引访问用户列,比如 user[8]
(我需要执行一次查询并检查列号),但是当我尝试指定
conn = mysql.connect(dictionary=True)
我得到了错误
'typeerror:cursor()得到意外的关键字参数'dictionary'
在使用mysql时,有没有一种方法可以获得字典格式的查询结果?
1条答案
按热度按时间fivyi3re1#
您可以遵循以下答案:https://stackoverflow.com/a/41388992/3129414
现有代码修改:
使用
DictCursor
在MySQL
初始化删除
dictionary=True
从光标使用
json
将字典项转换为json格式的包app.py
:截图: