Hi I'd like to get a table from a database, but include the field names so I can use them from column headings in e.g. Pandas where I don't necessarily know all the field names in advance
so if my database looks like
table test1
a | b | c
---+---+---
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
How can I do a
import psycopg2 as pq
cn = pq.connect('dbname=mydb user=me')
cr = cn.cursor()
cr.execute('SELECT * FROM test1;')
tmp = cr.fetchall()
tmp
such that tmp shows
[('a','b','c'),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3)]
Thanks
INFORMATION_SCHEMA
, but that's much more flexible as it isn't limited to just tables. – Dislikecr.description(test1)
doesn't work. If you put it into an answer I'd be happy to mark it up. – Serrano