Windows下SQLite命令行工具使用示例

SQLite官方有提供Windows下的SQLite命令行工具,最新版本为v3.7.11,下载解压后可得到sqlite3.exe。打开CMD窗口,定位到解压文件夹,执行"sqlite3.exe dbname"即可打开数据库并进入SQLite控制台[1][2],然后就可以执行各种命令了:

F:\>sqlite3.exe my.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

一、SQLite命令

1.以.开始的命令

这种命令是SQLite命令行工具自身的命令,比如.help显示帮助信息、.tables列出库所有表名等。

引用内容 引用内容
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
.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>

2.以;结尾的命令

这种命令是SQL语句,用来对数据库的表和记录进行增、删、改、查操作[3]。

sqlite> create table user(id integer primary key autoincrement, name text);
sqlite> insert into user(id,name) values(null,'user1');
sqlite> insert into user(name) values('user2');
sqlite> insert into user values(null,'user3');
sqlite> select id,name from user;
1|user1
2|user2
3|user3
sqlite>

二、补充说明

[1].当指定的dbname不存在时,sqlite3.exe会自动创建并打开它。dbname可用相对地址和绝对地址,例如:sqlite3.exe C:\my.db;
[2].执行sqlite3.exe my.db后,磁盘上没有马上创建my.db文件,直到第一次执行sql语句时才会创建;
[3].自动增长字段如果不用autoincrement关键字,字段值可能会和已删除的记录字段值重复,加了autoincrement关键字就不会出现这样的情况。

三、相关链接

@.SQLite Download:http://www.sqlite.org/sqlite-shell-win32-x86-3071100.zip
@.Command Line Shell For SQLite:http://www.sqlite.org/sqlite.html
@.Datatypes In SQLite Version 3:http://www.sqlite.org/datatype3.html

评论: 0 | 引用: 0 | 查看次数: 12816
发表评论
登录后再发表评论!