SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。
安装
下载地址:
Linux
apt install sqlite3
请访问 SQLite 下载页面,从源代码区下载 sqlite-autoconf-*.tar.gz
,编译步骤如下:
$ tar xvfz sqlite-autoconf-3071502.tar.gz
$ cd sqlite-autoconf-3071502
$ ./configure --prefix=/usr/local
$ make
$ make install
Windows
下载 sqlite-tools-win32-*.zip
和 sqlite-dll-win32-*.zip
压缩文件。
创建文件夹 C:\sqlite
,并在此文件夹下解压上面两个压缩文件,将得到 sqlite3.def
、sqlite3.dll
和 sqlite3.exe
文件。
添加 C:\sqlite
到 PATH
环境变量,最后在命令提示符下,使用 sqlite3
命令,将显示如下结果:
C:\>sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
安装 go-sqlite3
先安装mingw-w64-install.exe
,下载地址:https://sourceforge.net/projects/mingw-w64/files/
安装后,配置path路径,然后执行安装:
go build github.com/mattn/go-sqlite3
sqlite 管理工具
snap install sqlitebrowser
- DBhub share Public and private databases
命令行
SQLite
命令被称为 SQLite
的点命令,这些命令的不同之处在于它们不以分号 ;
结束。
让我们在命令提示符下键入一个简单的 sqlite3 命令,在 SQLite 命令提示符下,您可以使用各种 SQLite 命令。
$ sqlite3
SQLite version 3.24.0 2018-06-04 14:10:15
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .help
.auth ON|OFF Show authorizer callbacks
.backup ?DB? FILE Backup DB (default "main") to FILE
Add "--append" to open using appendvfs.
.bail on|off Stop after hitting an error. Default OFF
.binary on|off Turn binary output on or off. Default OFF
.cd DIRECTORY Change the working directory to DIRECTORY
.changes on|off Show number of rows changed by SQL
.check GLOB Fail if output since .testcase does not match
.clone NEWDB Clone data into NEWDB from the existing database
.databases List names and files of attached databases
.dbconfig ?op? ?val? List or change sqlite3_db_config() options
.dbinfo ?DB? Show status information about the database
.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
.eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN
.excel Display the output of next command in a spreadsheet
.exit Exit this program
.expert EXPERIMENTAL. Suggest indexes for specified queries
.fullschema ?--indent? Show schema and the content of sqlite_stat tables
.headers on|off Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.imposter INDEX TABLE Create imposter table TABLE on index INDEX
.indexes ?TABLE? Show names of all indexes
If TABLE specified, only show indexes for tables
matching LIKE pattern TABLE.
.limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT
.lint OPTIONS Report potential schema issues. Options:
fkey-indexes Find missing foreign key indexes
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
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 "|"
quote Escape answers as for SQL
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Use STRING in place of NULL values
.once (-e|-x|FILE) Output for the next SQL command only to FILE
or invoke system text editor (-e) or spreadsheet (-x)
on the output.
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE
The --new option starts with an empty file
Other options: --readonly --append --zip
.output ?FILE? Send output to FILE or stdout
.print STRING... Print literal STRING
.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
.save FILE Write in-memory database into FILE
.scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off
.schema ?PATTERN? Show the CREATE statements matching PATTERN
Add --indent for pretty-printing
.selftest ?--init? Run tests defined in the SELFTEST table
.separator COL ?ROW? Change the column separator and optionally the row
separator for both the output mode and .import
.session CMD ... Create or control sessions
.sha3sum ?OPTIONS...? Compute a SHA3 hash of database content
.shell CMD ARGS... Run CMD ARGS... in a system shell
.show Show the current values for various settings
.stats ?on|off? Show stats or turn stats on or off
.system CMD ARGS... Run CMD ARGS... in a system shell
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.testcase NAME Begin redirecting output to 'testcase-out.txt'
.timeout MS Try opening locked tables for MS milliseconds
.timer on|off Turn SQL timer on or off
.trace FILE|off Output each SQL statement as it is run
.vfsinfo ?AUX? Information about the top-level VFS
.vfslist List all available VFSes
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set column widths for "column" mode
Negative values right-justify
sqlite> .exit
查看默认配置
.show 命令查看 SQLite 命令提示符的默认设置:
sqlite> .show
echo: off
eqp: off
explain: auto
headers: off
mode: list
nullvalue: ""
output: stdout
colseparator: "|"
rowseparator: "\n"
stats: off
width:
filename: :memory:
sqlite>
确保:提示符与点命令之间没有空格,否则将无法正常工作。
打开数据库
sqlite3 <sqlite3 db name>
# demo
sqlite3 /tmp/test.db
格式化输出
您可以使用下列的点命令来格式化输出为本教程下面所列出的格式:
sqlite> .header on
sqlite> .mode column
sqlite> .timer on
sqlite> .show
echo: off
eqp: off
explain: auto
headers: on
mode: column
nullvalue: ""
output: stdout
colseparator: "|"
rowseparator: "\n"
stats: off
width:
filename: :memory:
sqlite>
数据类型
NULL
INTEGER
整数,根据值的大小存储在 1、2、3、4、6 或 8 字节中REAL
浮点值,存储为 8 字节的 IEEE 浮点数字TEXT
文本字符串,使用数据库编码(UTF-8、UTF-16BE 或 UTF-16LE)存储BLOB
blob 数据
创建表
sqlite> CREATE TABLE COMPANY(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
# 出入数据
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ('Allen', 25, 'Texas', 15000.00 );
查表
sqlite> .tables
account_bkuser djcelery_workerstate
查数据
sqlite> select * from account_user;
id password last_login is_superuser username chname company qq phone is_staff date_joined email
---------- ---------- -------------------------- ------------ ---------- ---------- ---------- ---------- ---------- ---------- -------------------------- ----------
1 2019-05-24 11:14:22.363068 1 admin 1 2019-05-24 11:14:22.363068
Run Time: real 0.001 user 0.000131 sys 0.000073
查表结构
sqlite> .schema account_bkuser
CREATE TABLE IF NOT EXISTS "account_bkuser" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "password" varchar(128) NOT NULL, "last_login" datetime NULL, "is_superuser" bool NOT NULL, "username" varchar(128) NOT NULL UNIQUE, "chname" varchar(254) NOT NULL, "company" varchar(128) NOT NULL, "qq" varchar(32) NOT NULL, "phone" varchar(64) NOT NULL, "is_staff" bool NOT NULL, "date_joined" datetime NOT NULL, "email" varchar(254) NOT NULL);
退出
sqlite> .exit