feat: add sqlite reference (#848)

This commit is contained in:
zhangym 2024-11-06 07:17:03 +08:00 committed by GitHub
parent af5e05b7d8
commit bbd2fa2ebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 263 additions and 0 deletions

View File

@ -231,6 +231,7 @@ Quick Reference
[Redis](./docs/redis.md)<!--rehype:style=background: rgb(198 47 44);-->
[Elasticsearch](./docs/elasticsearch.md)<!--rehype:style=background: rgb(36 187 177);-->
[Neo4j](./docs/neo4j.md)<!--rehype:style=background: rgb(86 86 123);-->
[SQLite](./docs/sqlite.md)<!--rehype:style=background: rgb(0 59 90);-->
<!--rehype:class=home-card-->
## 快捷键

25
assets/sqlite.svg Normal file
View File

@ -0,0 +1,25 @@
<svg viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
<g clip-path="url(#a)">
<path
d="M60 0C26.863 0 0 26.863 0 60v136c0 33.137 26.863 60 60 60h108.46c-1.779-5.243-3.172-13.546-3.956-23.023-.675-8.152-.899-17.171-.525-25.87-.101-1.286-.162-2.122-.162-2.122s-1.874-12.61-4.571-22.969c-1.194-4.598-2.553-8.756-3.98-11.166-.735-1.243.08-6.351 2.131-13.916 1.2 2.071 6.263 10.932 7.28 13.788 1.145 3.228 1.385 4.153 1.385 4.153s-2.777-14.272-7.333-22.6a303.417 303.417 0 0 1 3.542-11.066c1.578 2.767 5.165 9.121 6.461 12.132.12.279.228.541.307.757.184.522.339.965.478 1.371.731 2.118.909 2.792.909 2.792s-.271-1.386-.775-3.524c-.213-.903-.478-1.965-.773-3.096-1.148-4.409-2.892-10.181-5.125-14.704 5.732-29.794 24.181-68.807 44.057-89.868 1.713-1.814 3.434-3.511 5.158-5.041 11.619-10.3 23.392-13.78 33.115-5.118 7.034 6.267 9.912 14.925 9.917 24.89V60c0-33.137-26.863-60-60-60H60Z"
fill="#D9D9D9" />
<path
d="M60 0C26.863 0 0 26.863 0 60v136c0 33.137 26.863 60 60 60h108.46c-1.779-5.243-3.172-13.546-3.956-23.023-.675-8.152-.899-17.171-.525-25.87-.101-1.286-.162-2.122-.162-2.122s-1.874-12.61-4.571-22.969c-1.194-4.598-2.553-8.756-3.98-11.166-.735-1.243.08-6.351 2.131-13.916 1.2 2.071 6.263 10.932 7.28 13.788 1.145 3.228 1.385 4.153 1.385 4.153s-2.777-14.272-7.333-22.6a303.417 303.417 0 0 1 3.542-11.066c1.578 2.767 5.165 9.121 6.461 12.132.12.279.228.541.307.757.184.522.339.965.478 1.371.731 2.118.909 2.792.909 2.792s-.271-1.386-.775-3.524c-.213-.903-.478-1.965-.773-3.096-1.148-4.409-2.892-10.181-5.125-14.704 5.732-29.794 24.181-68.807 44.057-89.868 1.713-1.814 3.434-3.511 5.158-5.041 11.619-10.3 23.392-13.78 33.115-5.118 7.034 6.267 9.912 14.925 9.917 24.89V60c0-33.137-26.863-60-60-60H60Z"
fill="url(#b)" />
<path
d="M246.083 36.91c-9.723-8.662-21.496-5.183-33.115 5.118-1.724 1.53-3.445 3.227-5.158 5.04-19.876 21.063-38.325 60.075-44.057 89.869 2.233 4.523 3.977 10.295 5.125 14.704.295 1.131.56 2.193.773 3.096.504 2.138.775 3.524.775 3.524s-.178-.674-.909-2.792c-.139-.406-.294-.849-.478-1.371a15.467 15.467 0 0 0-.307-.757c-1.296-3.011-4.883-9.365-6.461-12.132a303.417 303.417 0 0 0-3.542 11.066c4.556 8.328 7.333 22.6 7.333 22.6s-.24-.925-1.385-4.153c-1.017-2.856-6.08-11.717-7.28-13.788-2.051 7.565-2.866 12.673-2.131 13.916 1.427 2.41 2.786 6.568 3.98 11.166 2.697 10.359 4.571 22.969 4.571 22.969s.061.836.162 2.122c-.374 8.699-.15 17.718.525 25.87.893 10.791 2.575 20.062 4.719 25.023l1.455-.793c-3.148-9.774-4.427-22.584-3.867-37.358.847-22.581 6.05-49.813 15.662-78.196 16.24-42.847 38.772-77.224 59.394-93.642-18.796 16.956-44.235 71.839-51.85 92.163-8.526 22.759-14.569 44.117-18.21 64.579 6.283-19.183 26.598-27.429 26.598-27.429s9.964-12.274 21.608-29.81c-6.975 1.589-18.428 4.309-22.265 5.919-5.658 2.371-7.183 3.18-7.183 3.18s18.33-11.15 34.056-16.198c21.628-34.025 45.19-82.362 21.462-103.505M28.52 49c-3.063 0-5.554.895-7.463 2.683-1.91 1.79-2.878 4.137-2.878 7.012 0 1.49.24 2.848.714 4.093.474 1.247 1.212 2.403 2.203 3.447.992 1.045 2.977 2.465 5.936 4.27 3.63 2.186 6.005 3.96 7.145 5.346 1.141 1.385 1.707 2.836 1.707 4.348 0 2.026-.675 3.642-2.064 4.858-1.391 1.215-3.251 1.82-5.558 1.82-2.433 0-4.552-.845-6.372-2.526-1.82-1.682-2.738-3.908-2.759-6.698H18V87.78h1.131c.346-.959.824-1.45 1.45-1.45.3 0 1.005.205 2.104.588 2.671.938 4.865 1.39 6.59 1.39 2.971 0 5.508-1.026 7.622-3.114 2.11-2.086 3.176-4.602 3.176-7.54 0-2.278-.704-4.305-2.084-6.071-1.381-1.769-4.066-3.826-8.08-6.19-3.452-2.046-5.694-3.713-6.728-5.013-1.036-1.298-1.568-2.733-1.568-4.309 0-1.704.634-3.072 1.885-4.093 1.252-1.022 2.891-1.528 4.943-1.528 2.309 0 4.233.676 5.756 2.037 1.521 1.363 2.401 3.252 2.66 5.68h1.132v-8.774h-1.053c-.128.447-.247.733-.357.861-.106.128-.311.196-.615.196-.366 0-1.019-.152-1.945-.45-1.985-.66-3.816-.999-5.499-.999Zm36.98 0c-3.733 0-7.121.868-10.183 2.605-3.066 1.734-5.493 4.14-7.285 7.207-1.79 3.066-2.68 6.323-2.68 9.793 0 4.662 1.547 8.816 4.665 12.456 3.12 3.638 6.858 5.905 11.195 6.777.992.51 2.41 1.826 4.268 3.956 2.093 2.404 3.864 4.138 5.32 5.17a16.074 16.074 0 0 0 4.684 2.292c1.661.499 3.457.744 5.4.744 2.351 0 4.457-.403 6.311-1.234l-.416-1.018a10.184 10.184 0 0 1-3.434.568c-1.642 0-3.3-.534-4.963-1.606-1.66-1.075-3.736-3.125-6.213-6.15-1.164-1.45-1.967-2.364-2.421-2.722 4.745-.915 8.648-3.184 11.691-6.816 3.043-3.63 4.565-7.774 4.565-12.417 0-5.514-1.988-10.147-5.935-13.925C76.12 50.9 71.262 49 65.5 49Zm23.085 0 .06 1.195c2.484 0 3.876.722 4.188 2.174.115.52.169 1.478.178 2.86l-.02 26.42c-.02 1.973-.305 3.233-.853 3.78-.549.543-1.476.882-2.819 1.018l-.06 1.195h24.773l.635-5.993h-1.131c-.323 1.632-1.064 2.802-2.243 3.466-1.183.67-3.273 1-6.293 1h-2.342c-2.717 0-4.289-.97-4.704-2.92a6.011 6.011 0 0 1-.12-1.253l.1-26.714c0-1.97.252-3.29.774-3.937.528-.643 1.48-1.004 2.858-1.096L101.507 49H88.585Zm-22.688 1.45c4.21 0 7.647 1.635 10.302 4.935 2.654 3.3 3.97 7.824 3.97 13.553 0 5.428-1.335 9.774-4.01 13.044-2.675 3.268-6.235 4.896-10.66 4.896-4.25 0-7.696-1.679-10.34-5.053-2.643-3.374-3.95-7.755-3.95-13.142 0-5.535 1.323-9.967 3.989-13.279 2.665-3.308 6.233-4.955 10.699-4.955Zm56.968 5.679c-.604 0-1.065.201-1.409.607-.353.404-.461.894-.338 1.489.119.576.449 1.082.973 1.508.521.425 1.083.646 1.687.646.584 0 1.03-.221 1.35-.646.32-.426.417-.931.298-1.508-.124-.595-.442-1.085-.933-1.489-.497-.406-1.044-.607-1.628-.607Zm16.336 4.054c-1.027 3.897-3.252 6.006-6.669 6.346l.039 1.116h3.99l-.079 13.2c.006 2.258.076 3.76.238 4.525.393 1.83 1.605 2.761 3.632 2.761 2.934 0 6.012-1.763 9.23-5.288l-.972-.822c-2.324 2.32-4.379 3.486-6.173 3.486-1.103 0-1.787-.626-2.045-1.86a4.658 4.658 0 0 1-.099-1.058l.04-14.944h6.093l-.059-1.782h-6.015v-5.68h-1.151Zm23.561 4.975c-3.408 0-6.181 1.632-8.336 4.877-2.145 3.25-2.809 6.85-1.965 10.81.496 2.32 1.484 4.12 2.997 5.387 1.51 1.267 3.426 1.9 5.716 1.9 2.133 0 5.103-.534 6.352-1.607 1.253-1.072 2.408-2.81 3.474-5.19l-.854-.881c-1.7 3.087-5.133 4.642-7.721 4.642-3.559 0-5.738-1.927-6.55-5.758a13.745 13.745 0 0 1-.239-1.567c4.234-.662 7.442-1.833 9.608-3.526 2.163-1.693 4.336-3.489 3.93-5.386-.242-1.127-.835-2.013-1.747-2.683-.924-.67-3.324-1.018-4.665-1.018Zm-37.257.137-7.344 1.665v1.292l2.54-.313c1.231 0 1.955.55 2.184 1.645.077.367.125.881.139 1.528l-.08 11.849c-.02 1.64-.205 2.593-.575 2.879-.374.287-1.36.43-2.958.43l-.04 1.117h11.652l-.02-1.116c-1.619 0-2.668-.126-3.136-.372-.46-.245-.776-.69-.913-1.371-.106-.492-.152-1.338-.159-2.507l.04-16.726h-1.33Zm35.392 2.194c.709 0 1.394.27 2.084.802.68.531 1.095 1.122 1.23 1.763.665 3.15-2.166 5.328-8.535 6.542-.182-2.297.222-4.383 1.251-6.268 1.021-1.883 2.351-2.84 3.97-2.84Z"
fill="#003B57" />
</g>
<defs>
<linearGradient id="b" x1="147.614" y1="5.239" x2="147.614" y2="241.763"
gradientUnits="userSpaceOnUse">
<stop stop-color="#97D9F6" />
<stop offset=".92" stop-color="#0F80CC" />
<stop offset="1" stop-color="#0F80CC" />
</linearGradient>
<clipPath id="a">
<rect width="256" height="256" rx="60" fill="#fff" />
</clipPath>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 6.9 KiB

237
docs/sqlite.md Normal file
View File

@ -0,0 +1,237 @@
SQLite 备忘清单
===
本备忘单旨在快速理解 [SQLite](https://sqlite.com/) 所涉及的主要概念提供了最常用的SQL语句供您参考。
入门
---
### 介绍
<!--rehype:wrap-class=row-span-3-->
SQLite 是遵守ACID的关系数据库管理系统它包含在一个相对小的C程序库中。与许多其它数据库管理系统不同SQLite不是一个客户端/服务器结构的数据库引擎,而是被集成在用户程序中。
SQLite遵守ACID实现了大多数SQL标准。它使用动态的、弱类型的SQL语法。它作为嵌入式数据库是应用程序如网页浏览器在本地/客户端存储数据的常见选择。它可能是最广泛部署的数据库引擎,因为它正在被一些流行的浏览器、操作系统、嵌入式系统所使用。同时,它有许多程序设计语言的语言绑定。
----
安装
---
### 安装方式
- 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` 等安装。
- macOS
`brew install sqlite` 安装
操作
---
### 连接数据库
**`SQLite` 通常不需要复杂的配置。创建数据库时如果文件不存在SQLite 会自动创建它。**
```bash
# 不存在则新建
>sqlite3 mydatabase.db
```
### 数据库操作
```shell
# 显示数据库名称及对应文件
sqlite> .databases
main: /home/user/sqlite/database.db r/w
# 显示已经设置的值
sqlite> .show
echo: off
eqp: off
explain: auto
headers: off
mode: list
nullvalue: ""
output: stdout
colseparator: "|"
rowseparator: "\n"
stats: off
width:
filename: api.db
# 以 sql 的形式 dump 数据库
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE api (
id INTEGER PRIMARY KEY AUTOINCREMENT,
host TEXT NOT NULL,
port INTEGER NOT NULL,
path TEXT NOT NULL
);
INSERT INTO api VALUES(1,'example.com',8080,'/api/v1');
# 备份数据库
sqlite> .backup back
# 备份单张表
sqlite> .dump user
# 退出
sqlite> .exit
```
### 数据表操作
```sh
# 创建表
sqlite> create table user(id integer primary key, name text);
# 查看所有表
sqlite> .tables
# 查看表结构
sqlite> .schema user
# 导入文件到表中
sqlite> .import user.csv user
# 设置查询显示列名称
sqlite> .head on
```
### 输出模式设置
```sh
# 设置输出模式为 csv
sqlite> .mode csv
sqlite> select * from api;
id,host,port,path
1,example.com,8080,/api/v1
# 输出为 markdown
sqlite> select * from api;
| id | host | port | path |
|----|-----------------|------|---------|
| 1 | example.com | 8080 | /api/v1 |
# 支持 ascii box column csv html insert json line list markdown qbox quote table tabs tcl 类型,省略展示
```
### 支持 sql
```sql
-- 常用 sql
-- 创建表
create table user(id integer primary key, name text);
-- 删除表
drop table user;
-- 重命名表
alter table user rename to user_new;
-- 插入
-- 单条
insert into user(name) values('test');
-- 多条
insert into user(name) values('test1'),('test2');
-- 查询
select * from user;
-- 去重查询
select distinct name from user;
-- 统计
select count(id) from user;
-- limit
select * from user limit 2;
-- 条件查询
select * from user where id > 1;
-- 模糊查询
select * from user where name like '%test%';
-- group by
select name, count(id) from user group by name;
-- 排序
select * from user order by id desc;
-- 聚合函数
select max(id) from user;
-- 更新
update user set name='test3' where id=1;
-- 删除
delete from user where id=1;
```
### 事务支持
**事务**具有原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)四个标准属性,缩写为 ACID。
```sql
-- 开始事务
begin transaction;
-- 操作
update user set name='test4' where id=1;
-- 回滚
rollback;
-- 提交
commit;
```
### 命令行
`.help`
|命令|描述|
|:---|:---|
|.backup ?DB? FILE |备份 DB 数据库(默认是 "main")到 FILE 文件。|
|.bail ON\|OFF |发生错误后停止。默认为 OFF。|
|.databases |列出数据库的名称及其所依附的文件。
|.dump ?TABLE? |以 SQL 文本格式转储数据库。如果指定了 TABLE 表,则只转储匹配 LIKE 模式的 TABLE 表。
|.echo ON\|OFF |开启或关闭 echo 命令。
|.exit |退出 SQLite 提示符。
|.explain ON\|OFF |开启或关闭适合于 EXPLAIN 的输出模式。如果没有带参数,则为 EXPLAIN on即开启 EXPLAIN。
|.header(s) ON\|OFF |开启或关闭头部显示。
|.help |显示消息。
|.import FILE TABLE |导入来自 FILE 文件的数据到 TABLE 表中。
|.indices ?TABLE? |显示所有索引的名称。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表的索引。
|.load FILE ?ENTRY? |加载一个扩展库。
|.log FILE\|off |开启或关闭日志。FILE 文件可以是 stderr标准错误/stdout标准输出
|.nullvalue STRING |在 NULL 值的地方输出 STRING 字符串。
|.output FILENAME |发送输出到 FILENAME 文件。
|.output stdout |发送输出到屏幕。
|.print STRING... |逐字地输出 STRING 字符串。
|.prompt MAIN CONTINUE |替换标准提示符。
|.quit |退出 SQLite 提示符。
|.read FILENAME |执行 FILENAME 文件中的 SQL。
|.schema ?TABLE? |显示 CREATE 语句。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表。
|.separator STRING |改变输出模式和 .import 所使用的分隔符。
|.show |显示各种设置的当前值。
|.stats ON\|OFF |开启或关闭统计。
|.tables ?PATTERN? |列出匹配 LIKE 模式的表的名称。
|.timeout MS |尝试打开锁定的表 MS 毫秒。
|.width NUM |NUM 为 "column" 模式设置列宽度。
|.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/>
参考资料
---
- [百科](https://zh.wikipedia.org/wiki/SQLite)
- [SQLite](https://www.sqlite.org/)
- [菜鸟教程](https://www.runoob.com/sqlite/sqlite-tutorial.html)