mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-24 12:31:14 +00:00
25 lines
647 B
Python
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()
|