Python MySQL 解压 cur.fetchall() 的结果

标签 python mysql

我在 Python 中有一个查询,当我使用 row = cur.fetchone()rows = cur.fetchall() 时,我无法正确解压值。

import pymysql.cursors
cur.execute(QUERY)
rows = cur.fetchall()
for row in rows:
    col1_data, col2_data, col3_data = row

我希望 col#_data 包含相应的值,但它只有标签。如果我执行 print(row) 我得到:

{'post_author': 'USERNAME', 'newposts': Decimal('52'), 'reposts': Decimal('50')}

如何解压 mysql 值?

最佳答案

像字典一样访问:

for row in rows:
    post_author = row['post_author']
    newposts = row['newposts']
    reposts = row['newposts']

关于Python MySQL 解压 cur.fetchall() 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50244737/

相关文章:

python - VS Code任务和Python虚拟环境

python - 什么相当于 Python 中的 JavaScript promise.all()?

土耳其语字符的python编码

mysql - 如何使用命令行将单个表导入mysql数据库

python - 无法使用灵活类型执行 reduce

python - 在 cython 数组中查找整数索引

php - 将 2 个集合合并到另一个集合中,并在两个 Laravel 中具有相同的值

mysql - Google Cloud SQL 实例始终处于维护状态和二进制日志问题

php - 在 Kannel 中实现交付报告

php - 在 php 中从 MYSQL 结果创建 3D 数组