2012-03-09 11 views
11

Polecenie "pragma table_info (" tablename ")" wyświetla informacje o kolumnach i "pragma foreign_key_list (" tablename ")" klucze obce. Jak wyświetlić inne więzy (sprawdź, unikatowe) tabeli? Tylko parsowanie pola "sql" tabeli "sqlite_master"?Czy istnieje sposób na uzyskanie ograniczeń tabeli w SQLite?

+2

Jest też "Pragma index_list ('tablename')" Zobacz http://www.sqlite.org /pragma.html#pragma_index_list – Nabab

+0

@Nabab powinieneś rozważyć dodanie tego jako odpowiedź –

Odpowiedz

6

Myślę, że jedynym sposobem jest zrobienie tego w taki sposób, jaki zasugerowałeś, parsowanie kolumny sql bazy danych sqlite_master.

kod

Python to zrobić:

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) 
Powiązane problemy