一 sqlite
简单地说SQLite数据库对应的就是一个文件(一般命名为xxx.db)。可以通过sqlite shell命令行来操作sqlite数据库,也可以在其他的语言中通过api来操作sqlite数据库。sqlite数据库的访问甚至不需要用户名和密码。
在windows上可以下载:sqlite-shell-win32-x86-3071300.zip , 其他平台可以下载对应的shell命令行。
帮助:
d:\sqlite_test>sqlite3.exe test.db
SQLite version 3.7.13 2012-06-11 02:05:22
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> .help
.backup ?DB? FILE Backup DB (default “main”) to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? … Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices ?TABLE? Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY? Load an extension library
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default “main”) from FILE
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.stats ON|OFF Turn stats on or off
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.timeout MS Try opening locked tables for MS milliseconds
.trace FILE|off Output each SQL statement as it is run
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 … Set column widths for “column” mode
.timer ON|OFF Turn the CPU timer measurement on or off
二 使用sqlite
1)打开或创建sqlite数据库
d:\sqlite_test>sqlite3.exe test.db
SQLite version 3.7.13 2012-06-11 02:05:22
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite>
2) 创建表及插入数据
sqlite> create table mytable(name varchar(10), age smallint);
sqlite> insert into mytable values(“itech”, 5);
sqlite> insert into mytable values(“jason”, 10);
sqlite> select * from mytable;
itech|5
jason|10
sqlite>
3)查看表及表的schema
sqlite> .tables
mytable
sqlite> .schema mytable
CREATE TABLE mytable(name varchar(10), age smallint);
sqlite>
4)数据库导出为ascii的sql文件 + 重新导入到新的数据库
d:\sqlite_test>echo .dump | sqlite3 test.db > test.db.dump
d:\sqlite_test>type test.db.dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE mytable(name varchar(10), age smallint);
INSERT INTO “mytable” VALUES(\’itech\’,5);
INSERT INTO “mytable” VALUES(\’jason\’,10);
COMMIT;
d:\sqlite_test>type test.db.dump | sqlite3 test2.db
d:\sqlite_test>sqlite3 test2.db
SQLite version 3.7.13 2012-06-11 02:05:22
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite> .tables
mytable
sqlite>
5)格式化输出
sqlite> .mode column
sqlite> .header on
sqlite> select * from mytable;
name age
———- ———-
itech 5
jason 10
sqlite>
完!