sqlite会自动维护一个系统表sqlite_master,该表存储了我们所创建的各个table, view, trigger等等信息。
sqlite_master表数据字段:
type: 类型,取值一般为table, view
name:
tbl_name: 表名
rootpage:
sql:创建表或者视图的sql语句,可以从该sql语句中判断某字段是否存在
sqlite_master表结构如下:
CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
);
例如:
select * from sqlite_master where type = 'table' and name = 't_cmpt_cp'
sql执行结果是:
1. 查询与判断表
查询sqlite中所有表,可用如下sql语句。
select name from sqlite_master where type='table' order by name;
我们可以通过如下语句查看这个内建表的所有记录:
select * from sqlite_master
执行结果:
由此可以进一步引申:判断指定的表是否存在,可以用如下语句:
select count(*) from sqlite_master where type='table' and name = 'yourtablename';
其中yourtablename表示你要判断的表名,如果查询结果大于0,表示该表存在于数据库中,否则不存在。