aicode/kdb/sqlor.py
2025-12-11 22:37:17 +08:00

36 lines
1.2 KiB
Python
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模块操作数据库
日期数据,在数据库中用date类型日期格式YYYY-MM-DD
时间戳数据在数据库中用timestamp类型格式YYYY-MM-DD hh:mm:ss.999
sor.R()
sor.sqlExe() 的select语句
如果ns中有'page'属性返回数据格式如下
{
"total" # 查询结果总记录数
"rows": page指定的页数据 缺省每页返回80条记录pagerows属性可设置每页记录数
}
否则返回全部记录的数组
```
from sqlor.dbpools import DBPools
from ahserver.serverenv import ServerEnv
# 假设当前模块名称为"mymodule"
async def subcoro(sor, pid):
sql = "select * from appcoodes_kw where parentid=${pid}$"
r = await sor.sqlExe(sql, {'pid': pid})
return r
async def sqlor_op():
db = DBPools()
env = ServerEnv()
dbname = env.get_module_dbname()
async with db.sqlorContext(dbname) as sor:
id = env.uuid()
# 事务中如果代码或sql失败全部滚回正常结束自动提交
await sor.C('user', {'id':id, 'username':'john'})
# 添加数据表“user”数据
await sor.D('user', {'id': 'yuewfiuwe'})
# await sor.U('user', {'id', 'email':'test@abc.com'})
# await sor.R('user', {'id': 'yuewfiuwe'})
return subcoro(sor, 'test_data')
```