发现一个很好用的 ORM 库 -- peewee
以下为简单示例
from peewee import * db = SqliteDatabase('test.db') # 定义表结构 class Person(Model): sex_set = ((0, '女'), (1, '男')) name = CharField(max_length=20, default='huyang', verbose_name='姓名') age = IntegerField(default=18, verbose_name='年龄') sex = CharField(choices=sex_set, default=1, verbose_name='性别') class Meta: database = db # 创建表格 def test_createTable(): Person.create_table() # 添加数据 def test_insert(): # 方式一 ''' p = Person(name='胡杨', age=22, sex=1) p.save() ''' # 方式二 Person.create(name='zhangsan', age=90, sex=1) # 方式三:批量添加 Person.insert_many([ {"name": "wangwu1", "age": 31, "sex": 0}, {"name": "wangwu2", "age": 32, "sex": 1}, ]).execute() # 修改数据 def test_update(): Person.update({'name': "lisi", 'sex': 0}).where(Person.id == 1).execute() # 删除数据 def test_delete(): Person.delete().where(Person.id == 4).execute() # 查询数据 def test_select(): res = Person.select().where(Person.sex == 1) for i in res: print(i.id, ' ', i.name) test_select()