Alex Rivera | Logout

How to get field names when running plain sql query in django

Asked 2009-04-30T06:49:33.850
18

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?

Edit
Report

1 Answer

16

According to PEP 249, you can try using cursor.description, but this is not entirely reliable.

answered 2009-04-30T07:30:21.567

Your Answer