python peewee用来干啥的
发布时间:2025-04-06 15:40:44

peeweee是一种轻量级ORM。采用sqlalchemy内核,采用纯python编写;
它提供了多种数据库访问,如 SqliteDatabase(file or memory)、MYSQLDatabase、PostgresqlDatabase。
比较SQLAlchemy和peewee
peewee
·优点:
DjangoAPI使其易于使用
轻量级的实现很容易与任何web框架集成
·缺点:
不支持自动化 schema 迁移
在线mysql表结构不能像Django那样产生结构化模型。
SQLAlchemy
·优点:
强大的API使代码强大和适应性
灵活的设计使复杂的查询很容易写出来
·缺点:
工作单位的概念并不常见
重量级 API,导致长学习曲线
使用
·安装
pipinstallpeewee
·根据sql生成模型
//在localhost中读取ershouche数据表,然后将模型生成到dbhost。.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 #连表查询,peeweeee也支持join语句 #album=Album.select().join(Dmoz).where( #(Album.title=="Thrive")& #(Dmoz.name=="Newsboys") #).get() #album.title="StepUptotheMicrophone" #album.save()
python培训视频众多,全部在python学习网,欢迎在线学习!
