sqlite3命令详解(下)
sqlite3提供了多个命令来查看数据库的schema
".tables"命令可以查看当前数据库所有的表
比如,示例14:
sqlite> .tables
tbl1
tbl2
sqlite>
".tables"和在list模式下执行下面的语句相似:
SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table','view') ORDER BY 1
实际上, 如果你查看sqlite3程序的源码(found in the source tree in the file src/shell.c),you'll find exactly the above query.
另外,".tables"命令后也可以跟一参数,它是一个pattern,这样命令就只列出表名和该参数匹配的表。
比如,示例14-1:
sqlite> .tables
.tables
android_metadata bookmarks system
bluetooth_devices secure
sqlite> .tables s%
.tables s%
secure sqlite_sequence system
sqlite>
".indices"命令列出指定表的所有indices(索引)。第一个参数为表的名字。
比如,示例15:
sqlite> .schema system
.schema system
CREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON C
ONFLICT REPLACE,value TEXT);
CREATE INDEX systemIndex1 ON system (name);
sqlite> .tables
.tables
android_metadata bookmarks system
bluetooth_devices secure
sqlite> .indices system
.indices system
sqlite_autoindex_system_1
systemIndex1
sqlite>
".schema"命令,在没有参数的情况,它会显示最初用于创建数据库的CREATE TABLE和CREATE INDEX的SQL语句。比如,示例16
".schema"命令可以包含一个参数,它是一个pattern,用于对表进行过滤,这时只会显示满足条件的表和所有索引的SQL语句。
比如,示例15和示例17.
示例16:
sqlite> .schema
.schema
CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE bluetooth_devices (_id INTEGER PRIMARY KEY,name TEXT,addr TEXT,chan
nel INTEGER,type INTEGER);
CREATE TABLE bookmarks (_id INTEGER PRIMARY KEY,title TEXT,folder TEXT,intent TE
XT,shortcut INTEGER,ordering INTEGER);
CREATE TABLE secure (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON C
ONFLICT REPLACE,value TEXT);
CREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON C
ONFLICT REPLACE,value TEXT);
CREATE INDEX bookmarksIndex1 ON bookmarks (folder);
CREATE INDEX bookmarksIndex2 ON bookmarks (shortcut);
CREATE INDEX secureIndex1 ON secure (name);
CREATE INDEX systemIndex1 ON system (name);
sqlite>
示例17:
sqlite> .schema s%
.schema s%
CREATE TABLE secure (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON C
ONFLICT REPLACE,value TEXT);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON C
ONFLICT REPLACE,value TEXT);
CREATE INDEX secureIndex1 ON secure (name);
CREATE INDEX systemIndex1 ON system (name);
sqlite>
".schema"命令功能和下面的语句相似:
SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type!='meta' ORDER BY tbl_name, type DESC, name
如果你传了一个参数给".schema",以表明只想得到表的schema而包括索引的schema,那么SQL语句应该如下:
SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type!='meta' AND sql
相关新闻>>
- 发表评论
-
- 最新评论 更多>>