gpt4 book ai didi

python mysql.connector DictCursor?

转载 作者:IT老高 更新时间:2023-10-28 21:07:14 24 4
gpt4 key购买 nike

在 Python mysqldb 中,我可以像这样将游标声明为字典游标:

cursor = db.cursor(MySQLdb.cursors.DictCursor) 

这将使我能够按名称引用 cursor 循环中的列,如下所示:

for row in cursor:   # Using the cursor as iterator 
city = row["city"]
state = row["state"]

是否可以使用此 MySQL 连接器创建字典游标? http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html

他们的例子只返回一个元组。

我想 MySQL 的创造者最终会为我们做这件事吗?

最佳答案

根据这篇文章,它可以通过将 'dictionary=True' 传递给游标构造函数来获得: http://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html

所以我尝试了:

cnx = mysql.connector.connect(database='bananas')
cursor = cnx.cursor(dictionary=True)

并得到:TypeError: cursor() got an unexpected keyword argument 'dictionary'

我试过了:

cnx = mysql.connector.connect(database='bananas')
cursor = cnx.cursor(named_tuple=True)

并得到:TypeError: cursor() got an unexpected keyword argument 'named_tuple'

我也试过这个:cursor = MySQLCursorDict(cnx)

但无济于事。很明显,我在这里使用了错误的版本,我怀疑我们只需要耐心等待 http://downloads.mysql.com/docs/connector-python-relnotes-en.a4.pdf 上的文档。表明这些新功能在撰写本文时处于 alpha 阶段。

关于python mysql.connector DictCursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com