python中的mysql orm
mporm的Python项目详细描述
_ __ ___ _____ ___ _ __ _ __ ___
| '_ ` _ \| __ \/ _ \| `__/| '_ ` _ \
| | | | | | |___| |_| | | | | | | | |
|_| |_| |_|_| \___/|_| |_| |_| |_|
mporm是一个用Python编写的ORM工具,只有基本的MySQL CRUD API(5.5+)简体中文
概述
功能
- [X]类似于GORM的API
- [X]自动使用
uuid
作为字段id
- [X]自动设置
created_at
和updated_at
安装
pip3 install mporm
快速启动
frommpormimportORM,DSN,Model,StrField,IntFieldORM.load(DSN(user="xxxx",password="xxxx"))classHero(Model):name=StrField()age=IntField()Hero.create()# CRUDHero.add(name="Thor",age=1000)Hero.where(name="Thor").set(age=1001).update()Hero.where(name="Thor").find()Hero.where(name="Thor").delete()Hero.drop()
连接到数据库
mporm只能连接mysql数据库,有两种不同的方式加载数据库配置
通过DSN加载
DSN加载的最小代码是writen as
frommpormimportORM,DSNORM.load(DSN(user="xxxx",password="xxxx"))
因为mporm将自动将其他配置设置为host
=“localhost”、port
=3306、database
=“test”、charset
=“utf8”
当然,您可以自己填写所有配置
从toml文件加载
您可以将所有配置写入toml文件,如
[database]user="xxxx"password="xxxx"host="xxxx"port=3306database="xxxx"charset="xxxx"
然后使用load_file
方法
frommpormimportORMORM.load_file("path/to/toml")
注意如果使用第二种方法,请记住所有6配置都需要写入toml文件。
表前缀
可以使用属性__prefix__
定义模型,例如:
frommpormimportModelclassHero(Model):__prefix__="Marvel"...Hero.create()
这将创建一个名为“惊奇英雄”的新表
CRUD接口
我们定义了一个类似于
classHero(Model):__prefix__="Marvel"name=StrField()age=IntField()
插入
有两种方法可供选择:
Hero.new(name="Thor",age=1000).insert()
或者只需使用
Hero.add(name="Thor",age=1000)
将要执行的sql语句是
insertinto`marvel_hero`(name,age)values('Thor',1000);
选择
查询
Hero.first()## select * from `marvel_hero` order by created_at limit 1;Hero.last()## select * from `marvel_hero` order by created_at desc limit 1;Hero.take()## select * from `marvel_hero` limit 1;
此外,他们还可以进行辩论
Hero.first(10)## select * from `marvel_hero` order by created_at limit 10;Hero.last(10)## select * from `marvel_hero` order by created_at desc limit 10;Hero.take(10)## select * from `marvel_hero` limit 10;
其中
Hero.where(name="Thor",age=1000).find()## select * from `marvel_hero` where name = 'Thor' and age = 1000;Hero.where(name="Thor",age=1000).findone()## select * from `marvel_hero` where name = 'Thor' and age = 1000 limit 1;
当然,可以选择指定的字段
Hero.where(name="Thor",age=1000).select("name").find()## select name from `marvel_hero` where name = 'Thor' and age = 1000;
或者您只需使用
Hero.where(name="Thor",age=1000).filter("name")## select name from `marvel_hero` where name = 'Thor' and age = 1000;
计数
Hero.where(name="Thor").count()## select count(id) from `marvel_hero` where name = 'Thor';
还提供自定义计数字段
Hero.where(name="Thor").count("age")## select count(age) from `marvel_hero` where name = 'Thor';
高级
订购
Hero.where(name="Thor").order("age",desc=True).find()## select * from `marvel_hero` where name = 'Thor' order by age desc;
限值
Hero.where(name="Thor").limit(10).find()## select * from `marvel_hero` where name = 'Thor' limit 10;
偏移量
Hero.where(name="Thor").offset(10).find()## select * from `marvel_hero` where name = 'Thor' offset 10;
当然,您可以像使用链条一样使用它们
Hero.where(name="Thor").order("age").limit(10).offset(10).select("name","age").find()## select name, age from `marvel_hero` where name = 'Thor' order by age asc limit 10 offset 10;
更新
Hero.where(name="Thor").set(age=1001).update()## update `marvel_hero` set age=1001 where name = 'Thor';
删除
Hero.where(name="Thor").delete()## delete from `marvel_hero` where name = "Thor";
注意方法insert()
update()
delete()
返回受影响的行数,方法find()
返回一个list-typed
查询结果,更不用说方法findone()
返回一个dict-typed
查询结果。
待办事项
[]喜欢的地方
[]其中或
[]其中-<;gt;
[]自定义SQL语句执行
贡献
您可以做任何事情来帮助交付更好的MPORM
许可证
@XJJ,2019年~ datetime.now()
发布于MIT License