加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 19:04:14 所属栏目:百科 来源:网络整理
导读:命令“pragma table_info(‘tablename’)”列出了列信息和“pragma foreign_key_list(‘tablename’)”外键. 如何显示表的其他约束(检查,唯一)? 只能解析表“sqlite_master”的字段“sql”? 我认为唯一的办法是按照你建议的方式来解析sqlite_master数据库
命令“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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读