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的实例对应行
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.
- 创建表结构
db.connect()
db.create_table([Person]) - 添加行
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()
- 修改行
grandma.name = 'Grandma L.'
grandma.save() # Update grandma's name in the database.
# Returns: 1
- 删除行
# The return value of delete_instance() is the number of rows removed from the database.
grandma.delete_instance()
- 查找行
grandma = Person.get(Person.name == 'Grandma L.')
或者Model.select()
排序 order_by
分页paginate,原理是使用limit 和offset,也就是物理分页
总数
社区活跃度:
peewee这个项目基本上就是coleifer大佬在维护。