命令 pragma table_info('tablename')
列出列信息和 pragma foreign_key_list('tablename')
外键。
如何显示表的其他约束(检查、唯一)?
只解析“sqlite_master”表的“sql”字段?
最佳答案
我认为唯一的方法就是按照你建议的方式,解析 sqlite_master 数据库的 sql 列。
执行此操作的 Python 代码:
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)
关于database - 有没有办法在 SQLite 中获取表的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636053/