The command pragma table_info('tablename')
lists the columns information and pragma foreign_key_list('tablename')
the foreign keys.
How can I display other constraints (check, unique) of a table?
Only parsing the field "sql" of the table "sqlite_master"?
Asked
Active
Viewed 1.3k times
25

Super Kai - Kazuya Ito
- 22,221
- 10
- 124
- 129

fran
- 1,119
- 1
- 10
- 27
-
2There's also "pragma index_list('tablename')" See http://www.sqlite.org/pragma.html#pragma_index_list – Nabab Feb 13 '13 at 01:02
-
@Nabab you should really consider adding that as an answer – Timo Huovinen Oct 26 '13 at 20:36
3 Answers
16
I think the only way is to do this is the way you suggested, parse the sql column of the sqlite_master database.
Python code to do this:
import sqlite3
con = sqlite3.connect("example.sqlite3")
cur = con.cursor()
cur.execute("select sql from sqlite_master where type='table' and name='example_table'")
schema = cur.fetchone()
con.close()
entries = [ tmp.strip() for tmp in schema[0].splitlines() if tmp.find("constraint")>=0 or tmp.find("unique")>=0 ]
for i in entries: print(i)

jftuga
- 1,913
- 5
- 26
- 49
2
In SQLite, this command below shows the details of a table including Table Constraints:
.schema table_name
And this command below shows the details of tables excluding Table Constraints:
PRAGMA table_info(table_name);

Super Kai - Kazuya Ito
- 22,221
- 10
- 124
- 129