".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>
比如,示例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,value TEXT);
CREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,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,255)">
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
SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type!='Meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%' ORDER BY substr(type,2,1),name
事实上".tables"的也是采用这种"LIKE"的方式,进行pattern查询的。
".databases"命令将列出当前connection中所有的数据库。
一般至少包含2个,一个是"main",the original database opened.另一个是"temp",the database used for temporary tables.
There may be additional databases listed for databases attached using the ATTACH statement.
The first output column is the name the database is attached with,and the second column is the filename of the external file.
This file can be converted back into a database by piping it back into sqlite3.
把一个数据库进行archival备份可以用如下的命令:
$ echo '.dump' | sqlite3 ex1 | gzip -c >ex1.dump.gz
重新构建数据库。只需要如下的语句:
$ zcat ex1.dump.gz | sqlite3 ex2
比如:
The ".explain" dot command can be used to set the output mode to "column" and
to set the column widths to values that are reasonable for looking at the output of an EXPLAIN command.
If any regular sql is prefaced by EXPLAIN,then the sql command is parsed and analyzed but is not executed.
Instead,the sequence of virtual machine instructions that would have been used to execute the sql command are returned like a query result.
For example:
sqlite> .explain
sqlite> explain delete from tbl1 where two<20;
addr opcode p1 p2 p3
---- ------------ ----- ----- -------------------------------------
0 ListOpen 0 0
1 Open 0 1 tbl1
2 Next 0 9
3 Field 0 1
4 Integer 20 0
5 Ge 0 2
6 Key 0 0
7 ListWrite 0 0
8 Goto 0 2
9 Noop 0 0
10 ListRewind 0 0
11 ListRead 0 14
12 Delete 0 0
13 Goto 0 11
14 ListClose 0 0
The ".timeout" command sets the amount of time that the sqlite3 program will wait for locks to clear on files it is trying to access before returning an error. The default value of the timeout is zero so that an error is returned immediately if any needed database table or index is locked.
如何以Shell脚本的方式使用sqlite3命令
另一种方式是
:以sql语句作为sqlite3的第二个参数,在执行sql操作。为
了方便, sqlite3允许在第一个参数数据库名后,再跟一个参数,来指定要执行的sql语句。
如果sqlite3带2个参数进行启动的话,第二个参数将做为sql传递给sqlite library来处理,返回结果将以list模式在标准输出中进行显示,然后sqlite3程序也退出了。
比如,
示例17:
# sqlite3 /data/data/com.android.providers.settings/databases/settings.db "select * from system;select * from system"
比如,
示例18:
$
sqlite3 ex1 'select * from tbl1' |
>
awk '{printf "<tr><td>%s<td>%s\n",$1,$2 }'
<tr><td>hello<td>10
<tr><td>goodbye<td>20
$
sql语句的结束符
一般sqlite3的sql语句的结束符是
分号";"
. 然而你在shell中运行sqlite3时你还可以使用 "
GO
" (大写) 或 "
/
"来作为一条sql语句结束的标志. 他们分别在sql Server和Oracle中被使用. 但是他在sqlite3_exec()不能使用shell会先把他们转化为分号";",然后再传递到该函数.
在源码中编译sqlite3