Reputation: 4174
I am trying to fetch data from a postgres table using psycopg2. Here is what i have done.
import psycopg2
con = psycopg2.connect("host=localhost dbname=crm_whatsapp user=odoo password=password")
cur = con.cursor()
sql = """SELECT * from tbl_hospital;"""
db_cursor.execute(sql)
hospital_data = db_cursor.fetchall()
print('hospital_data',hospital_data)
And the output is:
hospital_data [(1, 'hospital1', 1), (2, 'hospital2', 2), (3, 'hospital3', 3), (4, 'hospital4', 1)]
The output is not contains the cloumn header. I need that too. How can i get that.?
Upvotes: 0
Views: 76
Reputation: 11267
The cursor has the metadata in it.
From "Programming Python" by M. Lutz:
...
db_cursor.execute(sql)
colnames = [desc[0] for desc in db_cursor.description]
Upvotes: 1