本文實(shí)例講述了Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
ORM框架就是 object relation model,對(duì)象關(guān)系模型,用來(lái)實(shí)現(xiàn)把數(shù)據(jù)庫(kù)中的表 映射到 面向?qū)ο缶幊陶Z(yǔ)言中的類,不需要寫sql,通過操作對(duì)象就能實(shí)現(xiàn) 增刪改查。
ORM的基本技術(shù)有3種:
(1)映射技術(shù)
數(shù)據(jù)類型映射:就是把數(shù)據(jù)庫(kù)中的數(shù)據(jù)類型,映射到編程語(yǔ)言中的數(shù)據(jù)類型。比如,把數(shù)據(jù)庫(kù)的int類型映射到Python中的integer 類型。
類映射:把數(shù)據(jù)庫(kù)中的表,映射到面向?qū)ο缶幊陶Z(yǔ)言的類,這樣就不用寫sql,直接操作對(duì)象就可以了。
關(guān)系映射:關(guān)系型數(shù)據(jù)庫(kù)最大的特點(diǎn)在于實(shí)體之間的關(guān)系,也就是表之間通過主外鍵的設(shè)置,產(chǎn)生的關(guān)聯(lián),把這種關(guān)聯(lián)映射成編程語(yǔ)言中基于對(duì)象引用的關(guān)系連接。
(2)CURD技術(shù)
CURD就是增加、更新、檢索、刪除的意思,就是實(shí)現(xiàn)了數(shù)據(jù)庫(kù)中的增刪改查的功能。
(3)緩存技術(shù)
把數(shù)據(jù)庫(kù)中查詢到的數(shù)據(jù),以類對(duì)象的形式,存儲(chǔ)在內(nèi)存中,用的時(shí)候隨時(shí)提取。
在ORM查詢命令中,并不會(huì)去查詢數(shù)據(jù)庫(kù),而是當(dāng)真正要讀取數(shù)據(jù)時(shí),才到數(shù)據(jù)庫(kù)中去查數(shù)據(jù)。
一、安裝peewee
c:/Python27/Scripts>pip install peeweeCollecting peewee Downloading peewee-2.8.5.tar.gz (506kB) 100% |████████████████████████████████| 512kB 437kB/sInstalling collected packages: peewee Running setup.py install for peewee ... doneSuccessfully installed peewee-2.8.5
二、定義表到類的映射
先把表定義為類,保存為 orm.py,后面的代碼會(huì)引用這個(gè)模塊。
代碼如下:
# -*- coding:utf8 -*-from peewee import *db = SqliteDatabase('test.db')class BaseModel(Model): class Meta: database = dbclass Course(BaseModel): id = PrimaryKeyField() period = IntegerField() description = CharField() class Meta: order_by = ('title',) db_table = 'course'class Teacher(BaseModel): id = PrimaryKeyField() name = CharField(null = False) gender = BooleanField() address = CharField() course_id = ForeignKeyField(Course,to_field='id',related_name = "course") class Meta: order_by = ('name',) db_table = 'teacher'三、創(chuàng)建表、新增記錄
# -*- coding:utf8 -*-from orm import *Course.create_table()Teacher.create_table()Course.create(id = 1,title='經(jīng)濟(jì)學(xué)',period = 320,description='文科必修')Course.create(id = 2,title='大學(xué)語(yǔ)文',period = 300,description='所有學(xué)科必修')Course.create(id = 3,title='操作系統(tǒng)',period = 320,description='計(jì)算機(jī)必修')Course.create(id = 4,title='馬克思主義哲學(xué)',period = 320,description='必修')Teacher.create(id = 1,name = '張三',gender=True,address='...',course_id = 1)Teacher.create(id = 2,name = '李四',gender=False,address='-',course_id = 2)Teacher.create(id = 3,name = '王五',gender=True,address='=',course_id = 3)
四、更復(fù)雜的操作
# -*- coding:gbk -*-from orm import *#獲取1行數(shù)據(jù)record = Course.get(Course.title=='大學(xué)語(yǔ)文')print("課程:%s ,學(xué)時(shí): %d" %(record.title,record.period))#更新這行數(shù)據(jù)的period字段,保存record.period = 200record.save()print("學(xué)分改為:%d" % record.period)#獲取1行數(shù)據(jù),再刪除,如果不存在,則會(huì)報(bào)錯(cuò)record = Course.get(Course.title=='馬克思主義哲學(xué)')record.delete_instance()###查詢所有數(shù)據(jù)course = Course.select()###查詢符合條件的數(shù)據(jù),排序course = Course.select().where(Course.id <3).order_by(Course.period.desc())###計(jì)算平均值total = Course.select(fn.Avg(Course.period)).alias('avg_period')###更新數(shù)據(jù)Course.update(period=300).where(Course.id>2).execute()###關(guān)聯(lián)數(shù)據(jù)Record = Course.select().join(Teacher).where(Teacher.gender == True)操作完成后,可以用sqlite3 test.db ,然后用命令查看表數(shù)據(jù)。
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選