使用peewee增删查改数据库

peewee是一个轻量级的ORM框架,主要用来方便的操作数据库。
问题:
可以直接执行sql语句吗?
用数据库连接池吗?

膜拜大佬:
github => https://github.com/coleifer
blog => http://charlesleifer.com/blog/tags/
stackoverflow =>https://stackoverflow.com/users/254346/coleifer

ddl
dml

关联关系如下:
Modal类对应数据库里面的表
Field对应列
Model的实例对应行

ObjectCorresponds to…
Model classDatabase table
Field instanceColumn on a table
Model instanceRow in a database table

来自官网的例子

# Connect to a MySQL database on network.
mysql_db = MySQLDatabase('my_app', user='app', password='db_password',
                         host='10.1.0.8', port=3306)
from peewee import *

db = SqliteDatabase('people.db')

class Person(Model):
    name = CharField()
    birthday = DateField()

    class Meta:
        database = db # This model uses the "people.db" database.
  1. 创建表结构
    db.connect()
    db.create_table([Person])
  2. 添加行
from datetime import date
grandma = Person(name='Bob', birthday=date(1960, 1, 15))
grandma.save() # bob is now stored in the database
# Returns: 1

或者Model.create()

  1. 修改行
grandma.name = 'Grandma L.'
grandma.save()  # Update grandma's name in the database.
# Returns: 1
  1. 删除行
# The return value of delete_instance() is the number of rows removed from the database.


grandma.delete_instance()
  1. 查找行
grandma = Person.get(Person.name == 'Grandma L.')

或者Model.select()
排序 order_by

分页paginate,原理是使用limit 和offset,也就是物理分页
在这里插入图片描述
总数

社区活跃度:
peewee这个项目基本上就是coleifer大佬在维护。
在这里插入图片描述

posted @ 2022-03-06 10:38  叶常落  阅读(317)  评论(0编辑  收藏  举报