peewee模块
Peewee
Python中数据库与ORM主要做这几件事:
- 数据库方面由程序员设计表关系,主要是1v1,1vN,NvN;
- ORM做数据类型映射,将数据库表示的char/int等类型映射成Python对象,将数据映射到Python类对象,将数据表关系映射到类关系中;
- 实现CRUD和事务语句转换,将转化的SQL语句传递给数据库引擎,并将结果返回转为类对象;
- 缓存优化,数据以对象的形式存储在内存中。
创建
from peewee import *
from datetime import date
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
birthday = DateField()
class Meta:
database = db
with db:
Person.create_table()
bob = Person(name='bob', birthday=date(1990,1,1))
bob.save()
bob = Person.get(name == 'bob')
print(bob.birthday)
# 其它数据库创建
from peewee import *
# SQLite database using WAL journal mode and 64MB cache.
sqlite_db = SqliteDatabase('/path/to/app.db', pragmas={
'journal_mode': 'wal',
'cache_size': -1024 * 64})
# Connect to a MySQL database on network.
mysql_db = MySQLDatabase('my_app', user='app', password='db_password',
host='10.1.0.8', port=3316)
# Connect to a Postgres database.
pg_db = PostgresqlDatabase('my_app', user='postgres', password='secret',
host='10.1.0.9', port=5432)
get方法只能获取唯一一条,获取多条通过filter()方法
查询
# 官方查询示例,返回的是迭代器
tweets = (Tweet
.select(Tweet, User)
.join(User)
.order_by(Tweet.create_date.desc()))
插入
# 单条数据
res = (Facility
.insert(facid=9, name='Spa', membercost=20, guestcost=30,
initialoutlay=100000, monthlymaintenance=800)
.execute())
# 上面插入单条等效于
res = Facility(facid=9, name='Spa', membercost=20, guestcost=30,
initialoutlay=100000, monthlymaintenance=800)
res.save()
# 多条数据
data = [
{'facid': 9, 'name': 'Spa', 'membercost': 20, 'guestcost': 30,
'initialoutlay': 100000, 'monthlymaintenance': 800},
{'facid': 10, 'name': 'Squash Court 2', 'membercost': 3.5,
'guestcost': 17.5, 'initialoutlay': 5000, 'monthlymaintenance': 80}]
res = Facility.insert_many(data).execute()
更新数据
# 修改单列
res = (Facility
.update(initialoutlay=10000)
.where(Facility.name == 'Tennis Court 2')
.execute())
# 修改多列
nrows = (Facility
.update(membercost=6, guestcost=30)
.where(Facility.name.startswith('Tennis'))
.execute())
删除数据
nrows = Member.delete().where(Member.memid == 37).execute()
修改表定义
from playhouse.migrate import *
# 通过Migrator实例来修改
# Postgres example:
my_db = PostgresqlDatabase(...)
migrator = PostgresqlMigrator(my_db)
# SQLite example:
my_db = SqliteDatabase('my_database.db')
migrator = SqliteMigrator(my_db)
migrate(
migrator.add_column('some_table', 'title', title_field),
migrator.rename_column('story', 'mod_date', 'modified_date'),
migrator.drop_column('some_table', 'old_column'),
migrator.drop_not_null('story', 'pub_date'),
migrator.rename_table('story', 'stories_tbl'),
migrator.add_index('story', ('pub_date',), False),
migrator.drop_index('story', 'story_pub_date_status'),
migrator.create_index('some_table', ('new_column',)
)
playhouse提供方法用于将对象转换成字典
from playhouse.shortcuts import model_to_dict, dict_to_model
d = model_to_dict(bob)
print(d)
peewee模块的更多相关文章
- Python有关模块学习记录
1 pandas numpy模块 首先安装搭建好jupyter notebook,运行成功后的截图如下: 安装使用步骤(PS:确定Python安装路径和安装路径里面Scripts文件夹路径已经配置到环 ...
- peewee在flask中的配置
# 原文:https://blog.csdn.net/mouday/article/details/85332510 Flask的钩子函数与peewee.InterfaceError: (0, '') ...
- tornado+peewee-async+peewee+mysql(一)
前言: 需要异步操作MySQL,又要用orm,使用sqlalchemy需要加celery,觉得比较麻烦,选择了peewee-async 开发环境 python3.6.8+peewee-async0.5 ...
- peewee的简单使用
peewee的简单使用 peewee是一个轻量级的ORM框架,peewee完全可以应对个人或企业的中小型项目的Model层,上手容易,功能强大. 一.安装peewee模块 使用pip命令工具安装pee ...
- python 第三方模块 转 https://github.com/masterpy/zwpy_lst
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...
- [Python]peewee使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- python模块 - 常用模块推荐
http://blog.csdn.net/pipisorry/article/details/47185795 python常用模块 压缩字符 当谈起压缩时我们通常想到文件,比如ZIP结构.在Pyth ...
- [Python]peewee 使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- python模块大全
python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...
随机推荐
- 使用pipework将Docker容器桥接到本地网络环境中
在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主机的网卡桥接起来,再给Docker容器配上IP就可以了.pipew ...
- JAVA如何解压缩ZIP文档
代码片段: package org.yu.units; import java.io.Closeable; import java.io.File; import java.io.FileInputS ...
- [luoguP2221] [HAOI2012]高速公路(线段树)
传送门 考虑每一段对答案的贡献 用每一段的左端点来表示当前这一段,那么区间就变成了[1,n-1] 如果询问区间[l,r],其中一个点的位置为x,则它对答案的贡献为(x-l)*(r-x)*s[x](s[ ...
- 算法复习——带修改莫队(bzoj2453)
题目: Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A ...
- tomact和eclipse的关联
tomact和eclipse的关联有很多文档,这里说下下面的问题: 问题: tomact安装成功,点击startup.sh能正常访问,通过eclipse启动后,不能打开8080页面 解决: l 重 ...
- MATLAB(1)
前言 之前经常用MATLAB,却不小心停留在了舒适区,连基本的调试方法都没有掌握.本文主要是对MATLAB程序调试中的一般方法进行总结,也是自己学习的记录.全文大致分为三个段落: 1)代码内调试: 2 ...
- msp430项目编程37
msp430中项目---usb接口编程37 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- Redis数据结构之压缩列表
压缩列表是Redis为了节约内存而开发的,由一系列特殊编码的连续内存块组成的顺序型数据结构.一个压缩列表可以包含任意多个节点,每个节点可以保存一个字节数组或者一个整数值. 一.压缩列表结构1. 压缩列 ...
- 代码布局relativeLayout
后台布局 在ANDROID 开发中有时候我们需要在后台添加布局文件这里我们来说一下后台添加RelativeLayout文件的方式: RelativeLayout,顾名思义,就是以“相对”位置/对齐 ...
- AC日记——小书童——刷题大军 洛谷 P1926
题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人. 文言是火,点亮历史宫灯:历史是灯,照亮社会之路:社会是路,通向哲学大坑:哲学是坑,埋葬文科生 ...