how to print a sqlite table’s column names

these assume a database file named ./mydb, with a table named mytable:

from the sqlite shell

$ sqlite mydb
sqlite> pragma table_info(mytable);
sqlite> .q

from the shell command line

  • $ echo pragma table_info(mytable); | sqlite mydb

from python

import os, sys, sqlite3
dbfile = "./mydb"
thetable = "mytable"
db = sqlite3.connect(dbfile)
cursor = db.cursor()
cursor.execute("PRAGMA table_info(thetable)")
for row in cursor.fetchall():
        print row[1]
db.close()