# 项目描述
异步的 mysql ORM 。
# 安装与教程
安装:`pip install asymysql`
[教程](https://github.com/lcctoor/lccpy/blob/main/packages/asymysql/docs/doc.md)
# Bug提交、功能提议
您可以通过 [Github-Issues](https://github.com/lcctoor/lccpy/issues)、[微信](https://raw.githubusercontent.com/lcctoor/me/main/author/WeChatQR.jpg)、[技术交流群](https://raw.githubusercontent.com/lcctoor/me/main/ExchangeGroup/PythonTecQR.jpg) 与我联系。
# 关于作者
作者:许灿标
邮箱:lcctoor@outlook.com
[主页](https://github.com/lcctoor/me/blob/main/home.md) | [微信](https://raw.githubusercontent.com/lcctoor/me/main/author/WeChatQR.jpg) | [Python技术微信交流群](https://raw.githubusercontent.com/lcctoor/me/main/ExchangeGroup/PythonTecQR.jpg)
开源项目:[让 Python 更简单一点](https://github.com/lcctoor/lccpy#readme)
# 教程预览
导入:
```python
from aiomysql import connect
from asymysql import ORM, mc, mf
```
创建ORM:
```python
async def mkconn():
return await connect(
host = 'localhost',
port = 3306,
user = 'root',
password = '123456789'
)
orm = await ORM(mkconn) # 账户ORM
db = orm['泉州市'] # 库ORM
sheet = db['希望小学'] # 表ORM
```
新增数据:
```python
line1 = {'姓名': '小一', '年龄':11, '签到日期':'2023-01-11'}
line2 = {'姓名': '小二', '年龄':12, '签到日期':'2023-01-12'}
line3 = {'姓名': '小三', '年龄':13, '签到日期':'2023-01-13'}
line4 = {'姓名': '小四', '年龄':14, '签到日期':'2023-01-14'}
line5 = {'姓名': '小五', '年龄':15, '签到日期':'2023-01-15'}
line6 = {'姓名': '小六', '年龄':16, '签到日期':'2023-01-16'}
await sheet.insert(line1) # 添加1条数据
await sheet.insert([ line2, line3, line4, line5, line6 ]) # 批量添加
```
查询:
```python
await sheet[:] # 查询所有数据
await sheet[3] # 查询第3条数据
await sheet[mc.年龄>13][mc.姓名=='小五'][1] # 查询年龄大于13、且姓名叫'小五'的第1条数据
```
修改:
```python
data = {
'视力': 5.0,
'性别': '男',
'爱好': '足球,篮球,画画,跳绳'
}
await sheet.update(data)[2:5]
```
删除:
```python
# 删除年龄>=15的所有数据
await sheet[mc.年龄>=15].delete()[:]
# 删除年龄大于10、且喜欢足球的第2条数据
await sheet[mc.年龄>10][mc.爱好.re('足球')].delete()[2]
# 删除所有数据
await sheet.delete()[:]
```
比较运算:
| **代码** |
| -------------- |
| mc.年龄 > 10 |
| mc.年龄 >= 10 |
| mc.年龄 < 10 |
| ... |
成员运算:
| **代码** | **解释** |
| ---------------------------- | -------------------------------- |
| mc.年级.isin('初三', '高二') | 若字段值是传入值的成员,则符合 |
| mc.年级.notin(10, 30, 45) | 若字段值不是传入值的成员,则符合 |
过滤器的集合运算:
| **代码** | **解释** |
| ---------------------------------------------------------------------- | -------------- |
| [ mc.年龄>3 ][ mc.年龄<100 ] | 交集 |
| [ (mc.年龄<30) \| (mc.年龄>30) \| (mc.年龄==30) \| (mc.年龄==None) ] | 并集 |
| [ (mc.年龄>3) - (mc.年龄>100) ] | 差集 |
| [ ~(mc.年龄>100) ] | 补集 |
只返回姓名、年龄这2个字段:
```python
await sheet[mc.年级=='高一']['姓名','年龄'][:]
```
优先按年龄降序,其次按姓名升序,排序后返回第2\~4条数据:
```python
await sheet[mc.年级=='高一'].order(年龄=False, 姓名=True)[2:4]
```
执行原生SQL语句:
```python
data, cursor = await sheet.execute('select 姓名 from 希望小学 limit 1')
data
# >>> [{'姓名': '小一'}]
data, cursor = await sheet.execute('update 希望小学 set 爱好="编程" limit 3')
cursor.rowcount
# >>> 3
data, cursor = await sheet.execute("delete from 希望小学 limit 2")
cursor.rowcount
# >>> 2
sql = 'insert into 希望小学(姓名, 年龄) values (%s, %s)'
students = [('小七', 17), ('小八', 18)]
data, cursor = await sheet.executemany(sql, students)
cursor.lastrowid
# >>> 8
```
# 支持作者1元
asymysql 是一个免费的开源项目,由个人维护。
每个小的贡献,都是构成车轮的一份子,可以帮助保持车轮完美旋转。
![donationQR.jpg](https://raw.githubusercontent.com/lcctoor/me/main/donation/donationQR_1rmb_200_200.jpg)