在django中运行简单的sql查询时如何获取字段名称 [英] How to get field names when running plain sql query in django
本文介绍了在django中运行简单的sql查询时如何获取字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的一个django视图中,我使用plain sql(not orm)查询数据库并返回结果。
In one of my django views I query database using plain sql (not orm) and return results.
sql = "select * from foo_bar"
cursor = connection.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
我得到的数据很好,但不是列名。如何获取返回的结果集的字段名称?
I am getting the data fine, but not the column names. How can I get the field names of the result set that is returned?
推荐答案
根据 PEP 249 ,您可以尝试使用 cursor.description
,但这并不完全可靠。
According to PEP 249, you can try using cursor.description
, but this is not entirely reliable.
这篇关于在django中运行简单的sql查询时如何获取字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文