python中的mysql orm

mporm的Python项目详细描述


 _ __ ___  _____  ___  _ __  _ __ ___
| '_ ` _ \|  __ \/ _ \| `__/| '_ ` _ \
| | | | | | |___| |_| | |   | | | | | |
|_| |_| |_|_|    \___/|_|   |_| |_| |_|

mporm是一个用Python编写的ORM工具,只有基本的MySQL CRUD API(5.5+)简体中文


概述

功能

  • [X]类似于GORM的API
  • [X]自动使用uuid作为字段id
  • [X]自动设置created_atupdated_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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java:字符串。RTL设备语言用标志“+”格式化,数字后加符号   java GAE作为桌面应用程序(Swing)的服务提供商   java将InputStream转换为FileInputStream不适用于Apache POI   java外部Voronoi库“网格”:什么是草图和处理?   重载重写的泛型方法java   java显示组织上设置的错误。springframework。验证。jsp中的错误对象   java一些Spring模型属性没有显示在我的JSP中   java无法编译Guava 23的SimpleTimeLimiter示例   java如何更改JTree中的“根”目录名?   java如何在安卓中对相对布局产生连锁反应?   java错误:org。冬眠例外SQLGrammarException:无法提取结果集,dateAccessed是未知列   如何使用java监听JSON文件更新   由抽象封闭类创建的匿名内部类能否通过反射确定实现类?