database - 有没有办法在 SQLite 中获取表的约束?

标签 database list sqlite constraints pragma

命令 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/

相关文章:

mysql - 如何在mysql中获取前10行的总和?

mysql - 将数据库中的前 10 名球员打印到页面上的最简单方法是什么?

java - 为什么@Transactional 在我的代码中不起作用?

python - 如何迭代两个字典并将它们保存在python列表中

php - 基于具有不同值的相同列名创建列

python - 在 Python 中连接可变数量的列表

r - 将元素添加到 R 中向量的开头

SQLite - 是否可以使非整数主键工作?

android - 在数据库中删除/插入行后使用 ArrayAdapter 更新 ListView

python-3.x - 如何通过查询获取用户在 SQLite3 中的排名/位置?