python peewee用来干啥的

Python (292) 2023-05-22 00:06:07

peewee是一个轻量级的ORM。用的是sqlalchemy内核,采用纯python编写;

它提供了多种数据库的访问,如 SqliteDatabase(file or memory)、MYSQLDatabase、PostgresqlDatabase。

SQLAlchemy和peewee对比

peewee

·优点:

Django式的API,使其易用

轻量实现,很容易和任意web框架集成

·缺点:

不支持自动化 schema 迁移

不能像Django那样,使线上的mysql表结构生成结构化的模型。

SQLAlchemy

·优点:

巨牛逼的API,使得代码有健壮性和适应性

灵活的设计,使得能轻松写复杂查询

·缺点:

工作单元概念不常见

重量级 API,导致长学习曲线

使用

·安装

pipinstallpeewee

·根据sql生成模型

//读取localhost中的ershouche数据表,然后生成模型到db.py
python-mpwiz-emysql-Hlocalhost-p3306-uroot-Prootershouche>db.py

·增删查改

#coding=utf-8
fromdatetimeimportdatetime
fromdbimport*
database.connect()
#打印出所有元素
foriinDmoz.select():
printi.description
printi.__dict__
#增加数据
foriinrange(10):
printDmoz.create(description="user",link="HuaDong",title="100000%s"%str(i))
#删除数据
band=Dmoz.get(Dmoz.title=="1000001")
band.delete_instance()
#select语句
band=Dmoz.select().where(Dmoz.title=="1000000").get()
printband.link
#更改数据
band=Dmoz.get(Dmoz.title=="1000000")
printband.link
band.link="BeachBoys"
band.save()
printband.link
#连表查询,peewee也支持join语句
#album=Album.select().join(Dmoz).where(
#(Album.title=="Thrive")&
#(Dmoz.name=="Newsboys")
#).get()
#album.title="StepUptotheMicrophone"
#album.save()

众多python培训视频,尽在python学习网,欢迎在线学习!

THE END

发表回复