Python: PonyORM设计精巧的ORM框架

举报
彭世瑜 发表于 2021/08/13 23:05:03 2021/08/13
【摘要】 Pony is an advanced object-relational mapper 文档: PyPI: https://pypi.org/project/pony/Github: https://github.com/ponyorm/ponydoc: https://docs.ponyorm.org/ 安装 pip install pony 1 代码实例...

Pony is an advanced object-relational mapper

文档:

安装

pip install pony

  
 
  • 1

代码实例

# -*- coding: utf-8 -*-
from pony.orm import Database, db_session, Required, Set, select, set_sql_debug

# 创建数据库对象
db = Database()

# 建立数据库连接
db.bind(provider='sqlite', filename=':memory:')

# 开启debug,打印sql
set_sql_debug(True)

# 建立实体类
class Person(db.Entity): name = Required(str) age = Required(int) cars = Set('Car')


class Car(db.Entity): make = Required(str) model = Required(str) owner = Required(Person)


# 建表
db.generate_mapping(create_tables=True)


"""
CREATE TABLE "Person" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT,
  "name" TEXT NOT NULL,
  "age" INTEGER NOT NULL
)

CREATE TABLE "Car" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT,
  "make" TEXT NOT NULL,
  "model" TEXT NOT NULL,
  "owner" INTEGER NOT NULL REFERENCES "Person" ("id") ON DELETE CASCADE
)

SELECT "Car"."id", "Car"."make", "Car"."model", "Car"."owner"
FROM "Car" "Car"
WHERE 0 = 1

SELECT "Person"."id", "Person"."name", "Person"."age"
FROM "Person" "Person"
WHERE 0 = 1

"""

# 插入数据
with db_session: p1 = Person(name='John', age=20) p2 = Person(name='Mary', age=22) p3 = Person(name='Bob', age=30) c1 = Car(make='Toyota', model='Prius', owner=p2) c2 = Car(make='Ford', model='Explorer', owner=p3)
"""
INSERT INTO "Person" ("name", "age") VALUES (?, ?)
['John', 20]

INSERT INTO "Person" ("name", "age") VALUES (?, ?)
['Mary', 22]

INSERT INTO "Person" ("name", "age") VALUES (?, ?)
['Bob', 30]

INSERT INTO "Car" ("make", "model", "owner") VALUES (?, ?, ?)
['Toyota', 'Prius', 2]

INSERT INTO "Car" ("make", "model", "owner") VALUES (?, ?, ?)
['Ford', 'Explorer', 3]
"""


# 更新数据
with db_session: p = Person.get(id=2)  # 使用get来获取一条数据,如果没有查到会返回None p.name = "new name"

"""
SELECT "id", "name", "age"
FROM "Person"
WHERE "id" = ?
[2]

UPDATE "Person"
SET "name" = ?
WHERE "id" = ?
['new name', 2]
"""
# 查询数据
with db_session: for person in select(p for p in Person if p.age > 20)[:]: print(person.name, person.age, person.cars)

"""
SELECT "p"."id", "p"."name", "p"."age"
FROM "Person" "p"
WHERE "p"."age" > 20

SELECT "id", "make", "model", "owner"
FROM "Car"
WHERE "owner" = ?
[2]


SELECT "id", "make", "model", "owner"
FROM "Car"
WHERE "owner" = ?
[3]
"""

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

参考
只有 Python 魔法少女才知道的 PonyORM
抛弃丑陋,拥抱优雅–Pythonic的Pony ORM

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/116002969

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。