• 技术文章 >Python技术 >Python基础教程

    python peewee用来干啥的

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-11-30 09:35:42原创1942

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

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

    SQLAlchemy和peewee对比

    peewee

    ·优点:

    Django式的API,使其易用

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

    ·缺点:

    不支持自动化 schema 迁移

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

    SQLAlchemy

    ·优点:

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

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

    ·缺点:

    工作单元概念不常见

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

    使用

    ·安装

    pip install peewee

    ·根据sql生成模型

    //读取localhost中的ershouche数据表,然后生成模型到db.py
    python -m pwiz -e mysql -H localhost -p 3306 -u root -P root  ershouche > db.py

    ·增删查改

    #coding=utf-8
    from datetime import datetime
    from db import *
    database.connect()
    # 打印出所有元素
    for i in Dmoz.select():
        print i.description
        print i.__dict__
    # 增加数据
    for i in range(10):
        print Dmoz.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()
    print band.link
    # 更改数据
    band = Dmoz.get(Dmoz.title == "1000000")
    print band.link
    band.link = "Beach Boys"
    band.save()
    print  band.link
    # 连表查询,peewee也支持join语句
    # album = Album.select().join(Dmoz).where(
    #         (Album.title == "Thrive") & amp;
    # (Dmoz.name == "Newsboys")
    # ).get()
    # album.title = "Step Up to the Microphone"
    # album.save()

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

    专题推荐:python peewee
    品易云
    上一篇:Python:函数参数类型和排序的总结 下一篇:python nan是什么

    相关文章推荐

    • 什么是网络协议• python中的去除重复项的操作• python中少见的函数map()和partial()• python的sort()排序方法• Python中的文件读写-理论知识

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网