Python: PonyORM设计精巧的ORM框架

网友投稿 680 2022-05-30

Pony is an advanced object-relational mapper

文档:

PyPI: https://pypi.org/project/pony/

Github: https://github.com/ponyorm/pony

doc: https://docs.ponyorm.org/

安装

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

Python: PonyORM设计精巧的ORM框架

113

114

115

参考

只有 Python 魔法少女才知道的 PonyORM

抛弃丑陋,拥抱优雅–Pythonic的Pony ORM

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:IoT学习的必要性-谈谈个人的想法
下一篇:Python编程:sh模块执行shell命令
相关文章