|
| 1 | +SQLite 备忘清单 |
| 2 | +=== |
| 3 | + |
| 4 | +本备忘单旨在快速理解 [SQLite](https://sqlite.com/) 所涉及的主要概念,提供了最常用的SQL语句,供您参考。 |
| 5 | + |
| 6 | +入门 |
| 7 | +--- |
| 8 | + |
| 9 | +### 介绍 |
| 10 | +<!--rehype:wrap-class=row-span-3--> |
| 11 | + |
| 12 | +SQLite 是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程序库中。与许多其它数据库管理系统不同,SQLite不是一个客户端/服务器结构的数据库引擎,而是被集成在用户程序中。 |
| 13 | + |
| 14 | +SQLite遵守ACID,实现了大多数SQL标准。它使用动态的、弱类型的SQL语法。它作为嵌入式数据库,是应用程序,如网页浏览器,在本地/客户端存储数据的常见选择。它可能是最广泛部署的数据库引擎,因为它正在被一些流行的浏览器、操作系统、嵌入式系统所使用。同时,它有许多程序设计语言的语言绑定。 |
| 15 | + |
| 16 | +---- |
| 17 | + |
| 18 | +安装 |
| 19 | +--- |
| 20 | + |
| 21 | +### 安装方式 |
| 22 | + |
| 23 | +- windows |
| 24 | + |
| 25 | + 从 [SQLite](https://www.sqlite.org/download.html) 下载 |
| 26 | + |
| 27 | + 您需要下载 `sqlite-tools-win32-*.zip` 和 `sqlite-dll-win32-*.zip` 压缩文件。 |
| 28 | + |
| 29 | + 创建文件夹 `C:\sqlite`,并在此文件夹下解压上面两个压缩文件,将得到 `sqlite3.def、sqlite3.dll` 和 `sqlite3.exe` 文件。 |
| 30 | + |
| 31 | + 添加 `C:\sqlite` 到 `PATH` 环境变量。 |
| 32 | + |
| 33 | +- linux |
| 34 | + |
| 35 | + linux 自带 `sqlite3`,或者通过 `apt-get/yum/brew` 等安装。 |
| 36 | + |
| 37 | +- macOS |
| 38 | + |
| 39 | + `brew install sqlite` 安装 |
| 40 | + |
| 41 | +操作 |
| 42 | +--- |
| 43 | + |
| 44 | +### 连接数据库 |
| 45 | + |
| 46 | + **`SQLite` 通常不需要复杂的配置。创建数据库时,如果文件不存在,SQLite 会自动创建它。** |
| 47 | + |
| 48 | + ```bash |
| 49 | + # 不存在则新建 |
| 50 | + >sqlite3 mydatabase.db |
| 51 | + ``` |
| 52 | + |
| 53 | +### 数据库操作 |
| 54 | + |
| 55 | +```shell |
| 56 | +# 显示数据库名称及对应文件 |
| 57 | +sqlite> .databases |
| 58 | +main: /home/user/sqlite/database.db r/w |
| 59 | + |
| 60 | +# 显示已经设置的值 |
| 61 | +sqlite> .show |
| 62 | + echo: off |
| 63 | + eqp: off |
| 64 | + explain: auto |
| 65 | + headers: off |
| 66 | + mode: list |
| 67 | + nullvalue: "" |
| 68 | + output: stdout |
| 69 | +colseparator: "|" |
| 70 | +rowseparator: "\n" |
| 71 | + stats: off |
| 72 | + width: |
| 73 | + filename: api.db |
| 74 | + |
| 75 | +# 以 sql 的形式 dump 数据库 |
| 76 | +sqlite> .dump |
| 77 | +PRAGMA foreign_keys=OFF; |
| 78 | +BEGIN TRANSACTION; |
| 79 | +CREATE TABLE api ( |
| 80 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 81 | + host TEXT NOT NULL, |
| 82 | + port INTEGER NOT NULL, |
| 83 | + path TEXT NOT NULL |
| 84 | +); |
| 85 | +INSERT INTO api VALUES(1,'example.com',8080,'/api/v1'); |
| 86 | + |
| 87 | +# 备份数据库 |
| 88 | +sqlite> .backup back |
| 89 | + |
| 90 | +# 备份单张表 |
| 91 | +sqlite> .dump user |
| 92 | + |
| 93 | +# 退出 |
| 94 | +sqlite> .exit |
| 95 | +``` |
| 96 | + |
| 97 | +### 数据表操作 |
| 98 | + |
| 99 | +```sh |
| 100 | +# 创建表 |
| 101 | +sqlite> create table user(id integer primary key, name text); |
| 102 | + |
| 103 | +# 查看所有表 |
| 104 | +sqlite> .tables |
| 105 | + |
| 106 | +# 查看表结构 |
| 107 | +sqlite> .schema user |
| 108 | + |
| 109 | +# 导入文件到表中 |
| 110 | +sqlite> .import user.csv user |
| 111 | + |
| 112 | +# 设置查询显示列名称 |
| 113 | +sqlite> .head on |
| 114 | + |
| 115 | +``` |
| 116 | + |
| 117 | +### 输出模式设置 |
| 118 | + |
| 119 | +```sh |
| 120 | +# 设置输出模式为 csv |
| 121 | +sqlite> .mode csv |
| 122 | +sqlite> select * from api; |
| 123 | +id,host,port,path |
| 124 | +1,example.com,8080,/api/v1 |
| 125 | + |
| 126 | +# 输出为 markdown |
| 127 | +sqlite> select * from api; |
| 128 | +| id | host | port | path | |
| 129 | +|----|-----------------|------|---------| |
| 130 | +| 1 | example.com | 8080 | /api/v1 | |
| 131 | + |
| 132 | +# 支持 ascii box column csv html insert json line list markdown qbox quote table tabs tcl 类型,省略展示 |
| 133 | +``` |
| 134 | +
|
| 135 | +### 支持 sql |
| 136 | +
|
| 137 | +```sql |
| 138 | +-- 常用 sql |
| 139 | + |
| 140 | +-- 创建表 |
| 141 | +create table user(id integer primary key, name text); |
| 142 | + |
| 143 | +-- 删除表 |
| 144 | +drop table user; |
| 145 | + |
| 146 | +-- 重命名表 |
| 147 | +alter table user rename to user_new; |
| 148 | + |
| 149 | +-- 插入 |
| 150 | +-- 单条 |
| 151 | +insert into user(name) values('test'); |
| 152 | +-- 多条 |
| 153 | +insert into user(name) values('test1'),('test2'); |
| 154 | + |
| 155 | +-- 查询 |
| 156 | +select * from user; |
| 157 | +-- 去重查询 |
| 158 | +select distinct name from user; |
| 159 | +-- 统计 |
| 160 | +select count(id) from user; |
| 161 | +-- limit |
| 162 | +select * from user limit 2; |
| 163 | +-- 条件查询 |
| 164 | +select * from user where id > 1; |
| 165 | +-- 模糊查询 |
| 166 | +select * from user where name like '%test%'; |
| 167 | +-- group by |
| 168 | +select name, count(id) from user group by name; |
| 169 | +-- 排序 |
| 170 | +select * from user order by id desc; |
| 171 | +-- 聚合函数 |
| 172 | +select max(id) from user; |
| 173 | + |
| 174 | +-- 更新 |
| 175 | +update user set name='test3' where id=1; |
| 176 | + |
| 177 | +-- 删除 |
| 178 | +delete from user where id=1; |
| 179 | +``` |
| 180 | +
|
| 181 | +### 事务支持 |
| 182 | +
|
| 183 | +**事务**具有原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)四个标准属性,缩写为 ACID。 |
| 184 | +
|
| 185 | +```sql |
| 186 | +-- 开始事务 |
| 187 | +begin transaction; |
| 188 | +-- 操作 |
| 189 | +update user set name='test4' where id=1; |
| 190 | +-- 回滚 |
| 191 | +rollback; |
| 192 | +-- 提交 |
| 193 | +commit; |
| 194 | +``` |
| 195 | +
|
| 196 | +### 命令行 |
| 197 | +
|
| 198 | +`.help` |
| 199 | +
|
| 200 | +|命令|描述| |
| 201 | +|:---|:---| |
| 202 | +|.backup ?DB? FILE |备份 DB 数据库(默认是 "main")到 FILE 文件。| |
| 203 | +|.bail ON\|OFF |发生错误后停止。默认为 OFF。| |
| 204 | +|.databases |列出数据库的名称及其所依附的文件。 |
| 205 | +|.dump ?TABLE? |以 SQL 文本格式转储数据库。如果指定了 TABLE 表,则只转储匹配 LIKE 模式的 TABLE 表。 |
| 206 | +|.echo ON\|OFF |开启或关闭 echo 命令。 |
| 207 | +|.exit |退出 SQLite 提示符。 |
| 208 | +|.explain ON\|OFF |开启或关闭适合于 EXPLAIN 的输出模式。如果没有带参数,则为 EXPLAIN on,即开启 EXPLAIN。 |
| 209 | +|.header(s) ON\|OFF |开启或关闭头部显示。 |
| 210 | +|.help |显示消息。 |
| 211 | +|.import FILE TABLE |导入来自 FILE 文件的数据到 TABLE 表中。 |
| 212 | +|.indices ?TABLE? |显示所有索引的名称。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表的索引。 |
| 213 | +|.load FILE ?ENTRY? |加载一个扩展库。 |
| 214 | +|.log FILE\|off |开启或关闭日志。FILE 文件可以是 stderr(标准错误)/stdout(标准输出)。 |
| 215 | +|.nullvalue STRING |在 NULL 值的地方输出 STRING 字符串。 |
| 216 | +|.output FILENAME |发送输出到 FILENAME 文件。 |
| 217 | +|.output stdout |发送输出到屏幕。 |
| 218 | +|.print STRING... |逐字地输出 STRING 字符串。 |
| 219 | +|.prompt MAIN CONTINUE |替换标准提示符。 |
| 220 | +|.quit |退出 SQLite 提示符。 |
| 221 | +|.read FILENAME |执行 FILENAME 文件中的 SQL。 |
| 222 | +|.schema ?TABLE? |显示 CREATE 语句。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表。 |
| 223 | +|.separator STRING |改变输出模式和 .import 所使用的分隔符。 |
| 224 | +|.show |显示各种设置的当前值。 |
| 225 | +|.stats ON\|OFF |开启或关闭统计。 |
| 226 | +|.tables ?PATTERN? |列出匹配 LIKE 模式的表的名称。 |
| 227 | +|.timeout MS |尝试打开锁定的表 MS 毫秒。 |
| 228 | +|.width NUM |NUM 为 "column" 模式设置列宽度。 |
| 229 | +|.timer ON\|OFF |开启或关闭 CPU 定时器。 |
| 230 | +|.mode MODE | 设置输出模式,MODE 可以是下列之一:csv 逗号分隔的值 <br/>column 左对齐的列 <br/>html HTML 的 \<table\> 代码 <br/>insert TABLE 表的 SQL 插入(insert)语句 <br/>line 每行一个值 <br/>list 由 .separator 字符串分隔的值 <br/>tabs 由 Tab 分隔的值 <br/> tcl TCL 列表元素<br/> |
| 231 | +
|
| 232 | +
|
| 233 | +参考资料 |
| 234 | +--- |
| 235 | +- [百科](https://zh.wikipedia.org/wiki/SQLite) |
| 236 | +- [SQLite](https://www.sqlite.org/) |
| 237 | +- [菜鸟教程](https://www.runoob.com/sqlite/sqlite-tutorial.html) |
0 commit comments