使用SQL语句获取SQLite中的表定义
1. 问题提出
有人问,在sqlite中怎么用sql语句得到一个表的列定义语句。第一反应,可能就是用.schema <tablename>
可是这是sqlite的shell命令行。
使用代码,好像得不到结果。
幸好,sqlite它有比较特殊的系统表:
sqlite_master
sqlite_temp_master
这两张系统表的表结构完全相同,如下所示:
sqlite> .schema sqlite_masterCREATE TABLE sqlite_master ( type text, name text, tbl_name text, rootpage integer, sql text);sqlite> .schema sqlite_temp_masterCREATE TEMP TABLE sqlite_temp_master ( type text, name text, tbl_name text, rootpage integer, sql text);2. 实例说明
我们通过实例来说明,看看.schema相当于什么样的sql语句执行结果:
sqlite> create table t(id int);sqlite> create temporary table t(id int, col2 varchar(32));sqlite> insert into t values(1);Error: table t has 2 columns but 1 values were suppliedsqlite> drop table t;sqlite> .tablestsqlite> insert into t values(1);sqlite> create temporary table t(id int, col2 varchar(32));sqlite> insert into t values(2, 'wang');sqlite> .schema tCREATE TABLE t(id int);CREATE TABLE t(id int, col2 varchar(32));sqlite> select sql from sqlite_master where tbl_name='t';CREATE TABLE t(id int)sqlite> select sql from sqlite_temp_master where tbl_name='t';CREATE TABLE t(id int, col2 varchar(32))sqlite> select sql from sqlite_master where tbl_name='t' and type='table' union all select sql fromsqlite_temp_master where tbl_name='t' and type='table';CREATE TABLE t(id int)CREATE TABLE t(id int, col2 varchar(32))sqlite>我们看到,创建了两个表,都叫t, 一个是临时表,一个是非临时的。
创建完以后,在插入数据时,它优先认可临时表。
当我们运行.schema t时,会把两张表都列出来。
然后通过查询sqlite_master中的sql字段可以得到非临时表的建表信息,sqlite_temp_master中的sql字段能得到临时表的信息。两者的union就会得到所有表的信息。
所以:
.schema <t>
等价于:
select sql from sqlite_master where tbl_name='<t>' and type='table' union all select sql from
sqlite_temp_master where tbl_name='<t>' and type='table';
>更多相关文章
- 10-26Whitewidow SQL漏洞扫描工具演示
- 10-26SQL黑客注入防御与绕过的多种姿势
- 12-23SQLServer数据库操作总结(sql语法的使用)
- 12-21C#连接Sqlite
- 12-21ORACLE数据库学习之SQL性能优化详解
- 12-21解决SQLSERVER2008数据库日志文件占用硬盘空间问题
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 04-29通用智能人“通通”亮相中关村论坛
- 04-29拼多多投入45亿补贴,助力上海“五五购物节
- 04-29通义千问再开源 推出最大尺寸1100亿参数模型
- 04-29【环球视线】比亚迪交付首列出海云轨
- 04-21中国产品数字护照体系加速建设
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御