| | |
sqlite3-- how to see column names for table
![]() |
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
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
python Syntax (Toggle Plain Text)
con = sqlite3.connect( ":memory:" ) cur = con.cursor() cur.execute( "CREATE TABLE tablename ( d1 varchar, d2, varchar )" ) 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
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: And you should get back a printed list of tuples, where each tuple describes a column header.
Hope this helps!
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:
python Syntax (Toggle Plain Text)
cursor.execute("PRAGMA table_info(tablename)") print cursor.fetchall()
Hope this helps!
Vi veri veniversum vivus vici
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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.
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)
Python Syntax (Toggle Plain Text)
from sqlite3 import dbapi2 as sqlite cur.execute("SELECT * FROM SomeTable") 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)
![]() |
Other Threads in the Python Forum
- Previous Thread: Validate user input
- Next Thread: The tarfile module
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





