Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting database column names from query

Reply
Thread Tools

getting database column names from query

 
 
Jason Nordwick
Guest
Posts: n/a
 
      08-16-2006
I'm using MySQLdb and can connect and issue queries that return result sets, but I how do I get the column names for those result sets?

>>> c = MySQLdb.connect(*creds)
>>> k = c.cursor()
>>> k.execute("select * from account")

3L
>>> k.fetchall()

((1L, 'test', -1L), (2L, 'Test', -1L), (3L, 'Test2', -1L))


-j

 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      08-16-2006
Jason Nordwick wrote:
> I'm using MySQLdb and can connect and issue queries that return result sets, but I how do I get the column names for those result sets?
>
> >>> c = MySQLdb.connect(*creds)
> >>> k = c.cursor()
> >>> k.execute("select * from account")

> 3L
> >>> k.fetchall()

> ((1L, 'test', -1L), (2L, 'Test', -1L), (3L, 'Test2', -1L))
>
>
> -j


k.description

See also http://www.python.org/dev/peps/pep-0249/

HTH,
~Simon

 
Reply With Quote
 
 
 
 
Bob Kline
Guest
Posts: n/a
 
      08-16-2006
Jason Nordwick wrote:
> I'm using MySQLdb and can connect and issue queries that return result
> sets, but I how do I get the column names for those result sets?


[d[0] for d in k.description]

Cheers,
Bob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
1 Gridview. Dropdown A is column from database, Dropdown B is column from database, Data in A and B must be from same row. anonymoushamster@gmail.com ASP .Net 2 11-07-2007 12:40 PM
Repeater + Getting Column Names at Runtime Brent ASP .Net 4 09-15-2005 09:11 PM
Is it possible to get all the column names in a database table? Showjumper ASP .Net 1 02-21-2004 05:29 PM



Advertisments