使用peewee增删查改数据库
发布日期:2021-05-07 18:04:32 浏览次数:24 分类:精选文章

本文共 1238 字,大约阅读时间需要 4 分钟。

peewee是一个轻量级的ORM框架,主要用来方便的操作数据库。

问题:
可以直接执行sql语句吗?
用数据库连接池吗?

膜拜大佬:

github =>
blog =>
stackoverflow =>

ddl

dml

关联关系如下:

Modal类对应数据库里面的表
Field对应列
Model的实例对应行

Object Corresponds to…
Model class Database table
Field instance Column on a table
Model instance Row 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 dategrandma = 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大佬在维护。
在这里插入图片描述

上一篇:flask处理http request的时候是多线程还是多进程?
下一篇:逆向某网站sign签名算法

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年03月21日 08时48分59秒