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

作者:极客网 时间:2021-09-01 06:55:21 

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://docs.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)

来源:https://www.geek521.com/2021/02/04/python%E7%9A%84%E8%BD%BB%E9%87%8F%E7%BA%A7orm%E6%A1%86%E6%9E%B6peewee/

标签:python,ORM框架,peewee
0
投稿

猜你喜欢

  • golang 通过ssh代理连接mysql的操作

    2024-01-19 06:43:27
  • Python接口传输url与flask数据详解

    2023-03-18 20:00:42
  • Python数据分析之matplotlib绘图详解

    2022-11-21 16:34:30
  • Python解决两个整数相除只得到整数部分的实例

    2021-12-29 23:29:21
  • python实现股票历史数据可视化分析案例

    2021-12-20 00:29:10
  • python基于隐马尔可夫模型实现中文拼音输入

    2023-05-08 21:58:00
  • Golang学习之平滑重启

    2024-04-25 15:05:57
  • Python用内置模块来构建REST服务与RPC服务实战

    2023-05-09 19:32:16
  • JS实现拖动模糊框特效

    2023-08-06 05:18:51
  • MySql数据库查询中的特殊命令

    2024-01-16 20:22:11
  • Python实现分数序列求和

    2023-10-07 12:44:54
  • Python实现指定数组下标值正序与倒序排序算法功能举例

    2023-04-03 10:37:26
  • 详解如何用Python登录豆瓣并爬取影评

    2021-09-08 00:10:10
  • 用"表情符号"做植入广告 是否可行呢?

    2009-02-23 13:07:00
  • PHP7新特性foreach 修改示例介绍

    2024-06-05 09:23:48
  • python目标检测YoloV4当中的Mosaic数据增强方法

    2022-03-29 06:48:14
  • MySQL千万级数据的大表优化解决方案

    2024-01-18 06:24:02
  • 修改月光博客网站中PHP常用正则表达式中出现的错误

    2024-05-03 15:35:35
  • python生成器generator:深度学习读取batch图片的操作

    2022-07-31 08:43:06
  • Python如何实现定时器功能

    2023-04-13 23:19:28
  • asp之家 网络编程 m.aspxhome.com