peewee模块

Peewee

Python中数据库与ORM主要做这几件事:

  1. 数据库方面由程序员设计表关系,主要是1v1,1vN,NvN;
  2. ORM做数据类型映射,将数据库表示的char/int等类型映射成Python对象,将数据映射到Python类对象,将数据表关系映射到类关系中;
  3. 实现CRUD和事务语句转换,将转化的SQL语句传递给数据库引擎,并将结果返回转为类对象;
  4. 缓存优化,数据以对象的形式存储在内存中。

创建

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)

转载于:https://www.cnblogs.com/ikct2017/p/9431344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值