python_reference/sqlite3_howto/code/get_columnnames.py
Sebastian Raschka d1f0f9b4ad sqlite tutorial
2014-04-29 22:49:59 -04:00

25 lines
647 B
Python

# Sebastian Raschka, 2014
# Getting column names of an SQLite database table
import sqlite3
sqlite_file = 'my_first_db.sqlite'
table_name = 'my_table_3'
# Connecting to the database file
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
# Retrieve column information
# Every column will be represented by a tuple with the following attributes:
# (id, name, type, notnull, default_value, primary_key)
c.execute('PRAGMA TABLE_INFO({})'.format(table_name))
# collect names in a list
names = [tup[1] for tup in c.fetchall()]
print(names)
# e.g., ['id', 'date', 'time', 'date_time']
# Closing the connection to the database file
conn.close()