doc: update docs/sqlite.md (#848)
This commit is contained in:
parent
e9a61371f0
commit
a92759f0ed
185
docs/sqlite.md
185
docs/sqlite.md
@ -7,57 +7,58 @@ SQLite 备忘清单
|
|||||||
---
|
---
|
||||||
|
|
||||||
### 介绍
|
### 介绍
|
||||||
<!--rehype:wrap-class=row-span-3-->
|
|
||||||
|
|
||||||
SQLite 是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程序库中。与许多其它数据库管理系统不同,SQLite不是一个客户端/服务器结构的数据库引擎,而是被集成在用户程序中。
|
SQLite 是一个轻量级的嵌入式关系数据库管理系统,遵循 ACID 原则,广泛用于浏览器、操作系统等应用中,实现本地数据存储。
|
||||||
|
|
||||||
SQLite遵守ACID,实现了大多数SQL标准。它使用动态的、弱类型的SQL语法。它作为嵌入式数据库,是应用程序,如网页浏览器,在本地/客户端存储数据的常见选择。它可能是最广泛部署的数据库引擎,因为它正在被一些流行的浏览器、操作系统、嵌入式系统所使用。同时,它有许多程序设计语言的语言绑定。
|
### 安装
|
||||||
|
<!--rehype:wrap-class=col-span-2 row-span-2-->
|
||||||
|
|
||||||
----
|
#### windows
|
||||||
|
|
||||||
安装
|
- 从 [SQLite](https://www.sqlite.org/download.html) 下载两个压缩文件:`sqlite-tools-win32-*.zip`、`sqlite-dll-win32-*.zip`
|
||||||
---
|
- 创建文件夹 `C:\sqlite`,将这两个压缩文件解压到该文件夹下。
|
||||||
|
- 解压后,您将看到 3 个文件: `sqlite3.def`、 `sqlite3.dll`、 `sqlite3.exe`
|
||||||
|
- 将 C:\sqlite 添加到 PATH 环境变量中,以便在命令行中使用 SQLite。
|
||||||
|
<!--rehype:className=style-timeline-->
|
||||||
|
|
||||||
### 安装方式
|
#### linux
|
||||||
|
|
||||||
- windows
|
|
||||||
|
|
||||||
从 [SQLite](https://www.sqlite.org/download.html) 下载
|
|
||||||
|
|
||||||
您需要下载 `sqlite-tools-win32-*.zip` 和 `sqlite-dll-win32-*.zip` 压缩文件。
|
|
||||||
|
|
||||||
创建文件夹 `C:\sqlite`,并在此文件夹下解压上面两个压缩文件,将得到 `sqlite3.def、sqlite3.dll` 和 `sqlite3.exe` 文件。
|
|
||||||
|
|
||||||
添加 `C:\sqlite` 到 `PATH` 环境变量。
|
|
||||||
|
|
||||||
- linux
|
|
||||||
|
|
||||||
linux 自带 `sqlite3`,或者通过 `apt-get/yum/brew` 等安装。
|
linux 自带 `sqlite3`,或者通过 `apt-get/yum/brew` 等安装。
|
||||||
|
|
||||||
- macOS
|
#### macOS
|
||||||
|
|
||||||
`brew install sqlite` 安装
|
`brew install sqlite` 安装
|
||||||
|
|
||||||
操作
|
### 连接 SQLite 数据库
|
||||||
---
|
|
||||||
|
|
||||||
### 连接数据库
|
SQLite 通常无需复杂配置,当指定的数据库文件不存在时,它会自动创建一个新文件。
|
||||||
|
|
||||||
**`SQLite` 通常不需要复杂的配置。创建数据库时,如果文件不存在,SQLite 会自动创建它。**
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 不存在则新建
|
sqlite3 mydatabase.db
|
||||||
>sqlite3 mydatabase.db
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 数据库操作
|
若数据库文件不存在则会自动创建
|
||||||
|
|
||||||
|
数据库操作
|
||||||
|
---
|
||||||
|
|
||||||
|
### 显示数据库名称及对应文件
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# 显示数据库名称及对应文件
|
|
||||||
sqlite> .databases
|
sqlite> .databases
|
||||||
main: /home/user/sqlite/database.db r/w
|
main: /home/user/sqlite/database.db r/w
|
||||||
|
```
|
||||||
|
|
||||||
# 显示已经设置的值
|
### 备份数据库
|
||||||
|
|
||||||
|
```shell
|
||||||
|
sqlite> .backup back
|
||||||
|
```
|
||||||
|
|
||||||
|
### 显示已经设置的值
|
||||||
|
<!--rehype:wrap-class=row-span-3-->
|
||||||
|
|
||||||
|
```shell
|
||||||
sqlite> .show
|
sqlite> .show
|
||||||
echo: off
|
echo: off
|
||||||
eqp: off
|
eqp: off
|
||||||
@ -71,8 +72,24 @@ rowseparator: "\n"
|
|||||||
stats: off
|
stats: off
|
||||||
width:
|
width:
|
||||||
filename: api.db
|
filename: api.db
|
||||||
|
```
|
||||||
|
|
||||||
# 以 sql 的形式 dump 数据库
|
### 备份单张表
|
||||||
|
|
||||||
|
```shell
|
||||||
|
sqlite> .dump user
|
||||||
|
```
|
||||||
|
|
||||||
|
### 退出
|
||||||
|
|
||||||
|
```shell
|
||||||
|
sqlite> .exit
|
||||||
|
```
|
||||||
|
|
||||||
|
### 以 sql 的形式 dump 数据库
|
||||||
|
<!--rehype:wrap-class=col-span-2-->
|
||||||
|
|
||||||
|
```shell
|
||||||
sqlite> .dump
|
sqlite> .dump
|
||||||
PRAGMA foreign_keys=OFF;
|
PRAGMA foreign_keys=OFF;
|
||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
@ -83,60 +100,92 @@ CREATE TABLE api (
|
|||||||
path TEXT NOT NULL
|
path TEXT NOT NULL
|
||||||
);
|
);
|
||||||
INSERT INTO api VALUES(1,'example.com',8080,'/api/v1');
|
INSERT INTO api VALUES(1,'example.com',8080,'/api/v1');
|
||||||
|
|
||||||
# 备份数据库
|
|
||||||
sqlite> .backup back
|
|
||||||
|
|
||||||
# 备份单张表
|
|
||||||
sqlite> .dump user
|
|
||||||
|
|
||||||
# 退出
|
|
||||||
sqlite> .exit
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 数据表操作
|
### 导入与导出数据库
|
||||||
|
|
||||||
```sh
|
#### 导出数据库
|
||||||
# 创建表
|
<!--rehype:style=text-align: left;-->
|
||||||
sqlite> create table user(id integer primary key, name text);
|
|
||||||
|
|
||||||
# 查看所有表
|
```bash
|
||||||
sqlite> .tables
|
sqlite3 mydatabase.db .dump > backup.sql
|
||||||
|
```
|
||||||
|
|
||||||
# 查看表结构
|
#### 导入数据库
|
||||||
sqlite> .schema user
|
<!--rehype:style=text-align: left;-->
|
||||||
|
|
||||||
# 导入文件到表中
|
|
||||||
sqlite> .import user.csv user
|
|
||||||
|
|
||||||
# 设置查询显示列名称
|
|
||||||
sqlite> .head on
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sqlite3 mydatabase.db < backup.sql
|
||||||
```
|
```
|
||||||
|
|
||||||
### 输出模式设置
|
### 输出模式设置
|
||||||
|
<!--rehype:wrap-class=col-span-2-->
|
||||||
|
|
||||||
|
#### 设置输出模式为 csv
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# 设置输出模式为 csv
|
|
||||||
sqlite> .mode csv
|
sqlite> .mode csv
|
||||||
sqlite> select * from api;
|
sqlite> select * from api;
|
||||||
id,host,port,path
|
id,host,port,path
|
||||||
1,example.com,8080,/api/v1
|
1,example.com,8080,/api/v1
|
||||||
|
```
|
||||||
|
|
||||||
# 输出为 markdown
|
#### 输出为 markdown
|
||||||
|
|
||||||
|
```sh
|
||||||
sqlite> select * from api;
|
sqlite> select * from api;
|
||||||
| id | host | port | path |
|
| id | host | port | path |
|
||||||
|----|-----------------|------|---------|
|
|----|-----------------|------|---------|
|
||||||
| 1 | example.com | 8080 | /api/v1 |
|
| 1 | example.com | 8080 | /api/v1 |
|
||||||
|
|
||||||
# 支持 ascii box column csv html insert json line list markdown qbox quote table tabs tcl 类型,省略展示
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 支持 sql
|
支持 ascii box column csv html insert json line list markdown qbox quote table tabs tcl 等类型
|
||||||
|
|
||||||
|
数据表操作
|
||||||
|
---
|
||||||
|
|
||||||
|
### 常用表操作
|
||||||
|
|
||||||
|
#### 创建表
|
||||||
|
<!--rehype:style=text-align: left;color: var(--primary-color);-->
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sqlite> create table user(id integer primary key, name text);
|
||||||
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
#### 查看所有表
|
||||||
|
<!--rehype:style=text-align: left;color: var(--primary-color);-->
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sqlite> .tables
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 查看表结构
|
||||||
|
<!--rehype:style=text-align: left;color: var(--primary-color);-->
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sqlite> .schema user
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 导入文件到表中
|
||||||
|
<!--rehype:style=text-align: left;color: var(--primary-color);-->
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sqlite> .import user.csv user
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 设置查询显示列名称
|
||||||
|
<!--rehype:style=text-align: left;color: var(--primary-color);-->
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sqlite> .head on
|
||||||
|
```
|
||||||
|
|
||||||
|
### 常用 SQL
|
||||||
|
<!--rehype:wrap-class=col-span-2 row-span-2-->
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
-- 常用 sql
|
|
||||||
|
|
||||||
-- 创建表
|
-- 创建表
|
||||||
create table user(id integer primary key, name text);
|
create table user(id integer primary key, name text);
|
||||||
|
|
||||||
@ -193,9 +242,8 @@ rollback;
|
|||||||
commit;
|
commit;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 命令行
|
### 命令行帮助
|
||||||
|
<!--rehype:wrap-class=col-span-3-->
|
||||||
`.help`
|
|
||||||
|
|
||||||
|命令|描述|
|
|命令|描述|
|
||||||
|:---|:---|
|
|:---|:---|
|
||||||
@ -227,11 +275,14 @@ commit;
|
|||||||
|.timeout MS |尝试打开锁定的表 MS 毫秒。
|
|.timeout MS |尝试打开锁定的表 MS 毫秒。
|
||||||
|.width NUM |NUM 为 "column" 模式设置列宽度。
|
|.width NUM |NUM 为 "column" 模式设置列宽度。
|
||||||
|.timer ON\|OFF |开启或关闭 CPU 定时器。
|
|.timer ON\|OFF |开启或关闭 CPU 定时器。
|
||||||
|.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/>
|
|.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/>
|
||||||
|
<!--rehype:className=left-align-->
|
||||||
|
|
||||||
|
在命令行中通过 `.help` 命令显示帮助文档
|
||||||
|
|
||||||
|
另见
|
||||||
|
--------
|
||||||
|
|
||||||
参考资料
|
|
||||||
---
|
|
||||||
- [百科](https://zh.wikipedia.org/wiki/SQLite)
|
- [百科](https://zh.wikipedia.org/wiki/SQLite)
|
||||||
- [SQLite](https://www.sqlite.org/)
|
- [SQLite](https://www.sqlite.org/)
|
||||||
- [菜鸟教程](https://www.runoob.com/sqlite/sqlite-tutorial.html)
|
- [菜鸟教程](https://www.runoob.com/sqlite/sqlite-tutorial.html)
|
Loading…
x
Reference in New Issue
Block a user