python peewee模塊使用

版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

最近有個(gè)ETL取數(shù)項(xiàng)目, 存儲(chǔ)端使用數(shù)據(jù)庫(kù), 必要時(shí)候還要進(jìn)行多線(xiàn)程轉(zhuǎn)換, 而mysql 多線(xiàn)程多進(jìn)程讀寫(xiě)需要注意的地方太多, 多線(xiàn)程調(diào)試又非常麻煩。所有選擇python orm。

python 中orm模塊非常之多, 優(yōu)缺點(diǎn)也很明顯, 有功能比較全面的sqlalchemy, 也有django framework提供的ORM。 總之每個(gè)都優(yōu)點(diǎn), 但也單來(lái)缺點(diǎn)。 如sqlalchemy功能多, 相對(duì)學(xué)習(xí)曲線(xiàn)就長(zhǎng)了。 django framework提供的models操作, 有很多django特性, 但這些特性也能帶來(lái)缺點(diǎn)。

這里有一篇博客專(zhuān)門(mén)講了python 眾多ORM的選擇,

SQLAlchemy 和其他的 ORM 框架

本篇博客, 研究的是peewee。 超級(jí)輕量的一個(gè)ORM模塊, 而且操作數(shù)據(jù)非常之簡(jiǎn)單。

根據(jù)數(shù)據(jù)庫(kù)表生成模型

:::shell
python -m pwiz -e mysql -H localhost -p3306 -uroot -Pkkd93kd  web_db > db.py

模型樣例

:::python
from peewee import *

database = MySQLDatabase('web_db', **{'host': 'localhost', 'password': 'kkd93kd  ', 'port': 3306, 'user': 'root'})

class UnknownField(object):
    pass

class BaseModel(Model):
    class Meta:
        database = database
        
class SyShieldOrder(BaseModel):
    _id = PrimaryKeyField()
    order_up_day = IntegerField(null=True)
    order_up_hours = IntegerField(null=True)
    order_up_moon = IntegerField(null=True)
    order_up_quarter = IntegerField(null=True)
    order_up_week = IntegerField(null=True)
    order_up_year = IntegerField(null=True)
    tu_shopid = CharField(null=True)

    class Meta:
        db_table = 'sy_shield_order'
class SyShieldUser(BaseModel):
    _id = PrimaryKeyField()
    tu_account = CharField(null=True)
    tu_area = CharField(null=True)
    tu_city = CharField(null=True)
    tu_commence = DateField(null=True)
    tu_contract = DateField(null=True)
    tu_cost = IntegerField(null=True)
    tu_domain = CharField(null=True)
    tu_nick = CharField(null=True)
    tu_platform = CharField(null=True)
    tu_province = CharField(null=True)
    tu_realcost = IntegerField(null=True)
    tu_shopid = CharField(null=True)
    tu_version = CharField(null=True)

    class Meta:
        db_table = 'sy_shield_user'

操作

:::python

from datetime import datetime
from src import *

database.connect()

for i in SyShieldUser.select():
    print i.tu_account
    print i.__dict__

for i in range(10):
    data = {
            'tu_account': "user_%s" % str(i),
            'tu_area': "HuaDong",
            'tu_city': "Shanghai",
            }
    print SyShildUser.create(tu_account="user", tu_area="HuaDong", tu_city="Shanghai", tu_shopid="100000%s" % str(i))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,840評(píng)論 6 427
  • GitHub 上有一個(gè) Awesome - XXX 系列的資源整理,資源非常豐富,涉及面非常廣。awesome-p...
    若與閱讀 19,337評(píng)論 4 417
  • 環(huán)境管理管理Python版本和環(huán)境的工具。p–非常簡(jiǎn)單的交互式python版本管理工具。pyenv–簡(jiǎn)單的Pyth...
    MrHamster閱讀 3,961評(píng)論 1 61
  • 轉(zhuǎn)載,覺(jué)得這篇寫(xiě) SQLAlchemy Core,寫(xiě)得非常不錯(cuò)。不過(guò)后續(xù)他沒(méi)寫(xiě)SQLAlchemy ORM... ...
    非夢(mèng)nj閱讀 5,603評(píng)論 1 14
  • 2017年8月26日 如果不能最終擁有,那寧愿不要曾經(jīng)擁有。 真正擁有一件心愛(ài)的物品是在什么時(shí)候已經(jīng)忘記,但是那種...
    蔡毛閱讀 248評(píng)論 0 4

友情鏈接更多精彩內(nèi)容