日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Python學(xué)習(xí)路線介紹Peewee怎么用

 好程序員IT 2019-08-26

Python學(xué)習(xí)路線介紹Peewee怎么用,SQLAlchemy 功能很強(qiáng)大,文檔很豐富,是一個(gè)重量級(jí)的 ORM 框架。本文給大家介紹一個(gè)小清新,輕量級(jí) ORM 框架 Peewee,支持 Python 2.7+ 和 3.4+,支持 SQLite、MySQL 以及 PostgreSQL。如果對(duì) Django 的 ORM 比較熟悉,那么 Peewee 的學(xué)習(xí)成本會(huì)非常低。

安裝

pip install peewee

模型定義

from peewee import *
db = SqliteDatabase('people.db')
class BaseModel(Model):
    class Meta:
        database = db
class Person(BaseModel):
    name = CharField(verbose_name='姓名', max_length=10, null=False, index=True)
    gender = IntegerField(verbose_name='姓別', null=False, default=1)
    birthday = DateField(verbose_name='生日', null=True, default=None)
    class Meta:
        table_name = 'people'

首先定義了我們的模型類Person(用過(guò) Django 的同學(xué)一定對(duì)這種模型定義方式十分熟悉,跟 Django 中模型的定義十分相似),使用 SqliteDatabase指定了使用的數(shù)據(jù)庫(kù)people.db。

然后定義了Person這個(gè)表的數(shù)據(jù)字段,如果不指定主鍵,peewee會(huì)自動(dòng)幫我們創(chuàng)建一個(gè)id的字段作為主鍵。每一個(gè)Field都有幾個(gè)參數(shù)可以配置,長(zhǎng)度的大小,是否為空(null)和默認(rèn)值(default),索引(index)和唯一索引(unique)幾個(gè)常見(jiàn)的數(shù)據(jù)庫(kù)選項(xiàng)。

創(chuàng)建數(shù)據(jù)庫(kù)表

Person.create_table()
# 或
db.create_tables([Person])

操作數(shù)據(jù)庫(kù)

  • 直接創(chuàng)建實(shí)例,然后調(diào)用實(shí)例方法save()。

  • 也可以通過(guò)create()類方法創(chuàng)建實(shí)例并保存。

p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
p.save()
jerry = Person.create(name='jerry', gender=0, birthday=date(1999, 12, 1))
  • 使用delete().where().execute()進(jìn)行條件刪除,where()是刪除條件,execute()執(zhí)行刪除操作。

  • 如果是已經(jīng)查詢出來(lái)的實(shí)例對(duì)象,則調(diào)用實(shí)例方法delete_instance()進(jìn)行刪除。

# 刪除姓名為 tom 的數(shù)據(jù)
Person.delete().where(Person.name=='tom').execute()
# 已經(jīng)實(shí)例化的對(duì)象, 調(diào)用 delete_instance() 進(jìn)行刪除操作
p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
p.save()
p.delete_instance()
  • 使用update().wahere().excute()進(jìn)行條件更新。針對(duì)已經(jīng)查詢到的數(shù)據(jù)對(duì)象,在修改完對(duì)象屬性后,直接save()更新。

# 已經(jīng)實(shí)例化的對(duì)象,且擁有 id 這個(gè) primary key,則修改屬性后,save 即是更新操作
p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
p.save()
p.gender = 0
p.save()
# 更新 jerry 的 birthday 數(shù)據(jù)
q = Person.update({Person.birthday: date(1999, 12, 12)}).where(Person.name=='jerry')
q.execute()
  • 單條數(shù)據(jù)查詢使用Person.get(),也可以使用Person.select().where().get()。

  • 多條數(shù)據(jù)查詢使用Person.select().where()

# 查詢單條數(shù)據(jù)
p = Person.select().where(Person.name=='tom').get()
print(p.name, p.gender, p.birthday)
# 使用簡(jiǎn)寫 Model.get()
p = Person.get(Person.name=='tom')
print(p.name, p.gender, p.birthday)
# 查詢多條數(shù)據(jù)
people = Person.select().where(Person.gender==1)
for p in people:
    print(p.name, p.gender, p.birthday)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多