python - 我可以在python中使用名称列吗?
问题描述
我希望能够在 python 中使用我的 SQL 表。我设法使用代码连接和访问我的表。
我想知道是否可以在我的代码中检索列名。
我已经有了我的列:
ACTION | MANAGER
123 XYZ
456 ABC
PersonnesQuery = "SELECT........"
cursor = mydb.cursor()
cursor.execute(PersonnesQuery)
records_Personnes = cursor.fetchall()
for row in records_Personnes:
if (row["MANAGER"] == "XYZ"):
..................
elif (row["MANAGER"] == "ABC"):
................
引用的方式似乎不起作用。但是,它适用于数字,例如 row[0]。但我想按列名
解决方案
我假设您使用的是 pymysql 库,在这种情况下,有一种称为DictCursor的特定游标。这会将结果作为字典而不是列表返回。
cursor = mydb.cursor(pymysql.cursors.DictCursor)
cursor.execute(PersonnesQuery)
records_Personnes = cursor.fetchall()
推荐阅读
- javascript - 当尝试将 hompage 添加到 package.json 并运行 npm run build 时,它显示 /
- javascript - 如何在javascript中处理大型数组而不用块处理器阻塞UI
- sql - 使用密码加密存储过程
- python - 搜索推文,“此方法需要 GET 或 HEAD。”
- python - 为特定版本的 Python 安装包时出现问题
- php - 如何在多维数组中设置POST方法然后提交?
- python - cursor.execute 在暂存文件中有效,但在主文件中无效
- elasticsearch - elasticsearch 中的部分搜索适用于一个记录,而不适用于其他记录
- python-3.x - 如何使用 HTML-requests 库查找类名包含特定字符串的所有元素?
- wordpress - 如何在没有插件的情况下将 WordPress 帖子分享到 Instagram