-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
60 lines (42 loc) · 1.36 KB
/
Copy pathmodel.py
File metadata and controls
60 lines (42 loc) · 1.36 KB
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
from datetime import date
from peewee import *
db = SqliteDatabase("people.db")
class BaseModel(Model):
class Meta:
database = db
class Person(BaseModel):
# script = TextField()
name = CharField()
# is_success = BooleanField(default=False)
birthday = DateField() # 具有 year month day 属性
# time = TimeField() # 具有 hour minute second 属性
# DataTimeField() 上面都有
class Meta:
database = db # 这个 model 使用 "people.db" 数据库
# 若要显式指定模型类的表名, 可以使用 table_name 元选项
# table_name = 'tb_person'
db.connect()
db.create_tables([Person])
# 存储数据 save() 或者 create()
# 方式1
# sb_zz = Person(name="zz", birthday=date(1989, 9, 4))
# sb_zz.save()
# 方式二
# nc = Person.create(name="gg", birthday=date(1989, 6, 4))
# # 若要更新, 可以使用save
# nc.name = "sjb"
# nc.save()
# 查询
# # 获取单条记录, 使用 Select.get() 或者 Model.get()
p = Person.select().where(Person.name == "gg").get()
# p1 = Person.get(Person.name == "zz")
print(f"{p=}")
# # 获取所有清单
for person in Person.select():
print(f"{person.name=}")
# 删除实例
# # 比如先创建一个非智障的人, 需要删除
s = Person.create(name="xx", birthday=date(1977, 6, 6))
s.delete_instance()
# 处理完毕需要关闭数据库连接
db.close()