下载
sqldiff 或者 sqldiff.exe,SQLite 数据库比较工具;
sqlite3 或者 sqlite3.exe,SQLite 命令行客户端;
sqlite3_analyzer 或者 sqlite3_analyzer.exe,SQLite 数据表和索引的统计分析工具。
连接数据库
sqlite3.exe
SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
sqlite> .open hr.db
D:\Software\sqlite-tools-win32-x86-3330000\sqlite3.exe D:\Software\sqlite-tools-win32-x86-3330000\hr.db
SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite>
查看帮助
sqlite> .help
.archive ... Manage SQL archives
.auth ON|OFF Show authorizer callbacks
.backup ?DB? FILE Backup DB (default "main") to FILE
...
.vfslist List all available VFSes
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set minimum column widths for columnar output
sqlite> .help open
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE
Options:
--append Use appendvfs to append database to the end of FILE
--new Initialize FILE to an empty database
--nofollow Do not follow symbolic links
--readonly Open FILE readonly
--zip FILE is a ZIP archive
列出数据库
sqlite> .databases
main: D:\Software\sqlite-tools-win32-x86-3330000\hr.db
sqlite> attach database "D:\Software\sqlite-tools-win32-x86-3330000\new.db" as newdb;
sqlite> .databases
main: D:\Software\sqlite-tools-win32-x86-3330000\hr.db
newdb: D:\Software\sqlite-tools-win32-x86-3330000\new.db
查看数据库信息
sqlite> .dbinfo
database page size: 4096
write format: 1
read format: 1
reserved bytes: 0
file change counter: 161
database page count: 13
freelist page count: 0
schema cookie: 8
schema format: 4
default cache size: 0
autovacuum top root: 0
incremental vacuum: 0
text encoding: 1 (utf8)
user version: 0
application id: 0
software version: 3033000
number of tables: 3
number of indexes: 7
number of triggers: 0
number of views: 0
schema size: 1852
data version 1
备份数据库
sqlite> .backup backup.hr.db
sqlite> .save backup.hr.db
sqlite> .clone hr2.db
departments... done
jobs... done
employees... done
sqlite_autoindex_jobs_1... done
sqlite_autoindex_employees_1... done
emp_department_ix... done
emp_job_ix... done
emp_manager_ix... done
emp_name_ix... done
dept_location_ix... done
还原数据库
sqlite> .restore newdb backup.hr.db
列出所有表
sqlite> .tables
departments employees jobs newdb.t
sqlite> .tables 'emp%'
employees
查看表定义
sqlite> .schema --indent employees
CREATE TABLE employees(
employee_id INTEGER NOT NULL ,
first_name CHARACTER VARYING(20) ,
last_name CHARACTER VARYING(25) NOT NULL ,
email CHARACTER VARYING(25) NOT NULL ,
phone_number CHARACTER VARYING(20) ,
hire_date DATE NOT NULL ,
job_id CHARACTER VARYING(10) NOT NULL ,
salary NUMERIC(8,2) ,
commission_pct NUMERIC(2,2) ,
manager_id INTEGER ,
department_id INTEGER,
CONSTRAINT emp_emp_id_pk PRIMARY KEY(employee_id) ,
CONSTRAINT emp_salary_min CHECK(salary > 0) ,
CONSTRAINT emp_email_uk UNIQUE(email),
CONSTRAINT emp_dept_fk FOREIGN KEY(department_id) REFERENCES departments(department_id) ,
CONSTRAINT emp_job_fk FOREIGN KEY(job_id) REFERENCES jobs(job_id) ,
CONSTRAINT emp_manager_fk FOREIGN KEY(manager_id) REFERENCES employees(employee_id)
);
CREATE INDEX emp_department_ix ON employees(department_id);
CREATE INDEX emp_job_ix ON employees(job_id);
CREATE INDEX emp_manager_ix ON employees(manager_id);
CREATE INDEX emp_name_ix ON employees(last_name, first_name);
查看索引信息
sqlite> .indexes
dept_location_ix emp_name_ix
emp_department_ix sqlite_autoindex_employees_1
emp_job_ix sqlite_autoindex_jobs_1
emp_manager_ix
sqlite> .indexes jobs
sqlite_autoindex_jobs_1
sqlite> select * from newdb.sqlite_schema;
table|t|t|2|CREATE TABLE t(id int)
显示/修改当前设置
sqlite> .show
echo: off
eqp: off
explain: auto
headers: off
mode: list
nullvalue: ""
output: stdout
colseparator: "|"
rowseparator: "\n"
stats: off
width:
filename: hr.db
sqlite> .help echo
.help echo
.echo on|off Turn command echo on or off
sqlite> .echo on
.echo on
sqlite> select 1 as id;
select 1 as id;
1
设置输出格式
sqlite> .help mode
.mode MODE ?TABLE? Set output mode
MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
box Tables using unicode box-drawing characters
csv Comma-separated values
column Output in columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
json Results in a JSON array
line One value per line
list Values delimited by "|"
markdown Markdown table format
quote Escape answers as for SQL
table ASCII-art table
tabs Tab-separated values
tcl TCL list elements
sqlite> select 1 as id, "apple" as name;
id|name
1|apple
sqlite> .separator ", "
sqlite> select 1 as id, "apple" as name;
id, name
1, apple
sqlite> .mode box
sqlite> select 1 as id, "apple" as name;
┌────┬───────┐
│ id │ name │
├────┼───────┤
│ 1 │ apple │
└────┴───────┘
sqlite> .mode json
sqlite> select 1 as id, "apple" as name;
[{"id":1,"name":"apple"}]
sqlite> .mode markdown
sqlite> select 1 as id, "apple" as name;
| id | name |
|----|-------|
| 1 | apple |
sqlite> .mode insert product
sqlite> select 1 as id, "apple" as name;
INSERT INTO product(id,name) VALUES(1,'apple');
sqlite> .mode markdown
sqlite> .width 10 20
sqlite> select 1 as id, "apple" as name;
| id | name |
|------------|----------------------|
| 1 | apple |
设置 NULL 显示
sqlite> select "" as id, null as name;
id|name
|
sqlite> .nullvalue '[NULL]'
sqlite> select "" as id, null as name;
id|name
|[NULL]
显式执行时间
sqlite> select department_id,count(*) from employees group by 1;
department_id|count(*)
|1
10|1
20|2
30|6
40|1
50|45
60|5
70|1
80|34
90|3
100|6
110|2
Run Time: real 0.014 user 0.000000 sys 0.000000
显示执行计划
sqlite> .eqp on
sqlite> select count(*) from employees;
QUERY PLAN
`--SCAN TABLE employees USING COVERING INDEX emp_manager_ix
count(*)
107
执行脚本文件
sqlite> .read get_employees.sql
first_name|last_name
Ellen|Abel
Sundar|Ande
Mozhe|Atkinson
David|Austin
Hermann|Baer
select first_name, last_name
from employees
limit 5;
保存查询结果
sqlite> .output result.txt
sqlite> select * from jobs;
...
sqlite> .output
sqlite> select 1 as id;
id
1
导出 SQL 文件
sqlite3.exe hr.db .dump > hr.sql
恢复损坏的数据库
sqlite3.exe hr.db .recover > hr.sql
导入/导出 CSV
sqlite> .mode csv
sqlite> .import product.csv product
sqlite> select * from product;
id,name
1,apple
2,banana
3,orange
sqlite> .headers on
sqlite> .mode csv
sqlite> .once employees.csv
sqlite> SELECT * FROM employees;
sqlite> .system employees.csv
sqlite> .headers on
sqlite> .excel
sqlite> select * from employees;
SQLite 归档功能
选项 | 长选项 | 描述 |
---|---|---|
-c | --create | 创建一个新的归档。 |
-x | --extract | 从归档中提取文件。 |
-i | --insert | 增加文件到归档。 |
-t | --list | 列出归档中的文件。 |
-u | --update | 更新归档中的文件。 |
选项 | 长选项 | 描述 |
---|---|---|
-v | --verbose | 显示详细的处理过程。 |
-f FILE | --file FILE | 指定生成的归档文件,默认使用 main 数据库的文件。 |
-a FILE | --append FILE | 与 --file 参数类似,但是以追加方式打开归档。 |
-C DIR | --directory DIR | 指定相对路径所在的目录,默认为当前工作目录。 |
-n | --dryrun | 显示归档操作对应的 SQL 语句,不会实际执行操作。 |
-- | -- | 表示随后的内容都是命令行参数,而不是选项。 |
sqlite3 new_archive.db -Acv file1.txt file2.txt file3.txt
sqlite> .ar -cv -f new_archive.db file1.txt file2.txt file3.txt
file1.txt
file2.txt
file3.txt
sqlite> .ar --list -f new_archive.db
file1.txt
file2.txt
file3.txt
sqlite> .ar -x -f new_archive.db -C dir1 file1.txt
sqlite> .ar -c -f archive.zip file1.txt file2.txt file3.txt
读写二进制文件
sqlite> CREATE TABLE images(name TEXT, type TEXT, img BLOB);
sqlite> INSERT INTO images(name,type,img)
...> VALUES('icon','jpeg',readfile('icon.jpg'));
sqlite> SELECT writefile('icon.jpg',img) FROM images WHERE name='icon';
writefile('icon.jpg',img)
343618
sqlite> UPDATE docs SET body=edit(body, 'WINWORD.EXE') WHERE name='report-15';
执行系统命令
sqlite> .shell cd
D:\Software\sqlite-tools-win32-x86-3330000
sqlite> .system dir /B
get_employees.sql
hr.db
new.db
result.txt
sqldiff.exe
sqlite3.exe
sqlite3_analyzer.exe
退出客户端
sqlite> .exit