kivyterminal/py/gadget/docs/cn/sql.md
2023-12-05 11:11:59 +08:00

226 lines
4.9 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# sqlor
sqlor为web应用提供数据库接口支持 目前支持的数据库有:
* sqlite3
* mysqlmariadb
* postgresql
* gauss
* oracle
* sql server
本模块支持异步DBAPI接口目前测试过的有aiomysql aiopg
## sqlor 优点
* 支持多种数据库
* 支持异步减少了thread的开销
* 自动游标管理
* 自动事务管理
## 典型的应用模式
## sor 方法
### C(tablename, ns)
向添加表中添加一条记录
#### 例子
```
db = DBPools()
async with db.sqlorContext(dbname) as sor:
await sor.C(tablename, ns)
```
#### 参数说明
##### tablename
数据库表名称
##### ns
字典类型的数据字典的key为自发段名字典的value是字段值至少表主键和必须项要求数据
#### 返回值
### R(tablename, ns, filters=None)
按要求检索数据,支持分页数据检索和全部数据检索
#### 例子
```
db = DBPools()
ns = params_kw.copy()
async with db.sqlorContext(dbname) as sor:
r = await sor.R(tablename, ns)
```
#### 参数说明
##### tablename
数据表名
##### ns
字典数据,参数
##### filters
缺省为None 无过滤对象
否则是一个字典类型的过滤定义对象
#### 返回值
* 当ns中没有page属性时说明返回全部数据返回数组类型的数据列表
* 当ns中有page属性时返回
```
{
"total":记录数,
"rows":[一页的数据记录]
}
```
字典数据
### U(tablename, ns)
修改数据记录ns中的keys必须包含数据表的primary字段按照primary字段数据检索并修改数据记录
#### 例子
```
db = DBPools()
ns = params_kw.copy()
async with db.sqlorContext(dbname) as sor:
await sor.U(tablename, ns)
```
#### 参数说明
##### tablename
数据表名
##### ns
字典类型的数据字典的key为自发段名字典的value是字段值至少表主键和必须项要求数据
#### 返回值
### D(tablename, ns)
删除数据记录ns中的keys必须包含数据表的primary字段按照primary字段数据删除数据表记录
#### 例子
```
db = DBPools()
ns = params_kw.copy()
async with db.sqlorContext(dbname) as sor:
await sor.D(tablename, ns)
```
#### 参数说明
##### tablename
数据表名
##### ns
字典类型的数据字典的key为自发段名字典的value是字段值至少表主键数据
#### 返回值
### tables()
### I(tablename)
获得表的建表信息
#### 例子
```
db = DBPools()
async with db.sqlorContext(dbname) as sor:
r = await sor.I(tablename)
```
#### 参数说明
##### tablename
数据表名
### 返回值
如果数据表不存在返回None否则返回字典类型
返回值例子
```
{
"summary": [
{
"name": "user",
"title": "\u7528\u6237\u89d2\u8272",
"primary": [
"id",
"id"
]
}
],
"fields": [
{
"name": "id",
"type": "str",
"length": 32,
"dec": null,
"nullable": "no",
"title": "\u7528\u6237id",
"table_name": "user"
},
{
"name": "userid",
"type": "str",
"length": 32,
"dec": null,
"nullable": "yes",
"title": "\u7528\u6237id",
"table_name": "user"
},
{
"name": "rolerd",
"type": "str",
"length": 32,
"dec": null,
"nullable": "yes",
"title": "\u89d2\u8272id",
"table_name": "user"
}
],
"indexes": []
}
```
### sqlExe(sql,ns)
执行SQL语句
#### 例子
```
db = DBPools()
sql = "select * from mytable id=${id} country=${country}"
ns = params_kw.copy()
async with db.sqlorContext(dbname) as sor:
r = await sor.sqlExe(sql, ns)
```
sql中支持使用参数sqlor使用$[x}$引用变量sql中的变量需要在ns中能够找到
#### 参数说明
##### sql
字符串, 一个合法的sql语句字符串支持变量变量用${x}$格式引用ns中的属性.
##### ns
sql语句所需参数字典如果在ns中找不到sql中引用的变量将出错
#### 返回值
如果是“select” 语句,返回数组变量,包含所有找到的数据记录
其他类型的sql执行结果没有定义
如果出错返回None
### sqlPaging(sql, ns)
执行sql并分页返回数据
#### 例子
```
db = DBPools()
sql = "select * from mytable id=${id} country=${country}"
ns = params_kw.copy()
async with db.sqlorContext(dbname) as sor:
r = await sor.sqlPaging(sql, ns)
```
#### 参数说明
##### sql
字符串, 一个合法的sql语句字符串支持变量变量用${x}$格式引用ns中的属性.
##### ns
ns中必须包含一个sort属性说明sql结果数据的排序字段如果不指定函数将出错page参数如果不存在自动设置为1
##### 返回值
如果sql是一个select语句返回如下结构数据
```
{
"total":总记录数
"rows":[一页的数据]
}
```