• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python的轻量级ORM框架peewee使用教程

python 搞代码 4年前 (2022-01-07) 29次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了Python的轻量级ORM框架peewee使用教程,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

ORM框架使用最广泛的就是SQLAlchemy和Django自带的ORM框架,但是SQLAlchemy的语法显然相对Django的ORM框架麻烦一点。

而Django本身是一个web框架,比较重量级,仅仅为了使用Django的ORM框架的功能,而安装Django有点导致系统臃肿。而peewee这个框架语法几乎与Django的ORM框架一致,而又非常轻量。

它的安装非常简单:

 pip install peewee

如果你在使用mysql数据库的过程中报出如下错误:

 peewee.ImproperlyConfigured: MySQL driver not installed!

则需要安装一个mysql的驱动:

 pip install pymysql

peewee的whl包是880kB,pymysql的whl包是51KB,非常轻量级。

peewee的官方文档地址:http://d来源gao@daima#com搞(%代@#码网ocs.peewee-orm.com/en/latest/index.html

下面测试一下各项功能:

 from peewee import * db = MySQLDatabase('test', host="localhost", user='root', passwd='123456', port=3306) # 定义Person class Person(Model): name = CharField() birthday = DateField() is_relative = BooleanField() class Meta: database = db def test_create(): Person.create_table() # 创建多张表也可以这样 # database.create_tables([Person]) def test_insert(): # 添加一条数据 p = Person(name='小华', birthday=date(1996, 12, 20), is_relative=True) p.save() def test_delete(): # 删除姓名为perter的数据 Person.delete().where(Person.name == 'perter').execute() # 已经实例化的数据, 使用delete_instance p = Person(name='小华', birthday=date(1996, 12, 20), is_relative=False) p.id = 1 p.save() p.delete_instance() def test_update(): # 已经实例化的数据,指定了id这个primary key,则此时保存就是更新数据 p = Person(name='小华', birthday=date(1996, 12, 20), is_relative=False) p.id = 1 p.save() # 更新birthday数据 q = Person.update({Person.birthday: date(1983, 12, 21)}).where(Person.name == '小华') q.execute() def test_query(): # 查询单条数据 p = Person.get(Person.name == '小华') print(p.name, p.birthday, p.is_relative) # 使用where().get()查询 p = Person.select().where(Person.name == '小华').get() print(p.name, p.birthday, p.is_relative) # 查询多条数据 persons = Person.select().where(Person.is_relative == True) for p in persons: print(p.name, p.birthday, p.is_relative)

下面测试一个各个方法。

测试创建表:

 if __name__=="__main__": Person.create_table()

执行完毕,检查数据库成功创建下面这张表:

测试插入数据:

 if __name__=="__main__": p = Person(name='小华', birthday=date(1996, 12, 20), is_relative=True) p.save()

执行完毕后,表数据多了一行:

测试查询数据:

 if __name__=="__main__": p = Person.get(Person.name == '小华') print(p.name, p.birthday, p.is_relative)

结果:

 小华 1996-12-20 True

测试删除数据:

 if __name__=="__main__": Person.delete().where(Person.name == '小华').execute()

执行后,数据库对应的记录被删除:

测试修改数据:

 if __name__ == "__main__": p = Person(name='小新', birthday=date(1995, 6, 20), is_relative=False) p.save() # 更新birthday数据 q = Person.update({Person.birthday: date(1983, 5, 21)}).where(Person.name == '小新') q.execute()

测试批量查询:

 if __name__ == "__main__": for i in range(1, 5): p = Person(name=f'小张{i}', birthday=date(1995, 6, 20), is_relative=False) p.save() # 查询多条数据 persons = Person.select().where(Person.is_relative == False) for p in persons: print(p.name, p.birthday, p.is_relative)

以上就是Python的轻量级ORM框架peewee使用教程的详细内容,更多关于Python的轻量级ORM框架peewee的资料请关注gaodaima搞代码网其它相关文章!

以上就是Python的轻量级ORM框架peewee使用教程的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python的轻量级ORM框架peewee使用教程

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址