[Tutor] Getting at sqlite schema info from within Python?

Terry Carroll carroll at tjc.com
Tue Jun 26 07:23:08 CEST 2007


Is there any way of getting to the schema of an sqlite database from 
within Python? In particular, a list of tables in the DB.

>From the sqlite command line, I can use the sqlite 

 .tables

command to get a list of tables in the database; and then the 

 PRAGMA TABLE_INFO(tablename)

to get the information on the various fields in the table.

>From within Python, though, I can't find a way to get the table names.  
If I know the table names, the PRAGMA approach works to get at the field
info; but I can't figure out how to find the table names first.

Trying to use the sqlite .tables command via Python gives me:

>>> c.execute(".tables;")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near ".": syntax error

...which makes some sense; it isn't SQL.



More information about the Tutor mailing list