sqlite3-- how to see column names for table

Reply

Join Date: May 2008
Posts: 6
Reputation: jmroach is an unknown quantity at this point 
Solved Threads: 0
jmroach jmroach is offline Offline
Newbie Poster

sqlite3-- how to see column names for table

 
0
  #1
May 15th, 2008
So this should be a really easy one, but i've spent a few hours searching and can't find anything that explicitly states how to do this. All i want to do is see the column names for a particular table. I created the tables like:

  1. con = sqlite3.connect( ":memory:" )
  2. cur = con.cursor()
  3. cur.execute( "CREATE TABLE tablename ( d1 varchar, d2, varchar )" )
  4. cur.execute( "INSERT INTO tablename values (?,?)", tuple(datarow) )

since the tables are created dynamically i want be able to see the column headers; 'd1' and 'd2' in this case. isn't there an easy way to do this???

Thanks in advance.

-john
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: sqlite3-- how to see column names for table

 
0
  #2
May 15th, 2008
Hi jmroach,

You're going to want to use an sqlite pragma statement. A pragma statement lets you query database meta-data or in some cases alter the behavior of the database. The pragma that gives you column headers is called "table_info." In the example you gave below, you would use it by saying:
  1. cursor.execute("PRAGMA table_info(tablename)")
  2. print cursor.fetchall()
And you should get back a printed list of tuples, where each tuple describes a column header.

Hope this helps!
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 6
Reputation: jmroach is an unknown quantity at this point 
Solved Threads: 0
jmroach jmroach is offline Offline
Newbie Poster

Re: sqlite3-- how to see column names for table

 
0
  #3
May 15th, 2008
Perfect. Thanks a ton, G-Do!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: sqlite3-- how to see column names for table

 
0
  #4
May 28th, 2008
This seemed inefficient to me, so I looked around for another way. Here is a simpler way to do it, using the sqlite3.Cursor.description attribute.
  1. from sqlite3 import dbapi2 as sqlite
  2. cur.execute("SELECT * FROM SomeTable")
  3. col_name_list = [tuple[0] for tuple in cur.description]

cur.description returns a tuple of information about each table. The entire tuple is : (name, type_code, display_size, internal_size, precision, scale, null_ok)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: weisburd is an unknown quantity at this point 
Solved Threads: 0
weisburd weisburd is offline Offline
Newbie Poster

Re: sqlite3-- how to see column names for table

 
0
  #5
Feb 20th, 2009
bugmenot - thank you! That saved me a bunch of time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC