欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解

 更新時(shí)間:2017年07月20日 11:23:37   作者:不想長(zhǎng)大啊  
這篇文章主要介紹了Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ORM框架的概念、功能及peewee的安裝、使用及操作sqlite數(shù)據(jù)庫的方法,需要的朋友可以參考下

本文實(shí)例講述了Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

ORM框架就是 object relation model,對(duì)象關(guān)系模型,用來實(shí)現(xiàn)把數(shù)據(jù)庫中的表 映射到 面向?qū)ο缶幊陶Z言中的類,不需要寫sql,通過操作對(duì)象就能實(shí)現(xiàn) 增刪改查。

ORM的基本技術(shù)有3種:

(1)映射技術(shù)

數(shù)據(jù)類型映射:就是把數(shù)據(jù)庫中的數(shù)據(jù)類型,映射到編程語言中的數(shù)據(jù)類型。比如,把數(shù)據(jù)庫的int類型映射到Python中的integer 類型。
類映射:把數(shù)據(jù)庫中的表,映射到面向?qū)ο缶幊陶Z言的類,這樣就不用寫sql,直接操作對(duì)象就可以了。
關(guān)系映射:關(guān)系型數(shù)據(jù)庫最大的特點(diǎn)在于實(shí)體之間的關(guān)系,也就是表之間通過主外鍵的設(shè)置,產(chǎn)生的關(guān)聯(lián),把這種關(guān)聯(lián)映射成編程語言中基于對(duì)象引用的關(guān)系連接。

(2)CURD技術(shù)

CURD就是增加、更新、檢索、刪除的意思,就是實(shí)現(xiàn)了數(shù)據(jù)庫中的增刪改查的功能。

(3)緩存技術(shù)

把數(shù)據(jù)庫中查詢到的數(shù)據(jù),以類對(duì)象的形式,存儲(chǔ)在內(nèi)存中,用的時(shí)候隨時(shí)提取。

在ORM查詢命令中,并不會(huì)去查詢數(shù)據(jù)庫,而是當(dāng)真正要讀取數(shù)據(jù)時(shí),才到數(shù)據(jù)庫中去查數(shù)據(jù)。

一、安裝peewee

c:\Python27\Scripts>pip install peewee
Collecting peewee
 Downloading peewee-2.8.5.tar.gz (506kB)
  100% |████████████████████████████████| 512kB 437kB/s
Installing collected packages: peewee
 Running setup.py install for peewee ... done
Successfully installed peewee-2.8.5

二、定義表到類的映射

先把表定義為類,保存為 orm.py,后面的代碼會(huì)引用這個(gè)模塊。

代碼如下:

# -*- coding:utf8 -*-
from peewee import *
db = SqliteDatabase('test.db')
class BaseModel(Model):
  class Meta:
    database = db
class Course(BaseModel):
  id = PrimaryKeyField()
  title = CharField(null = False)
  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é)語文',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é)語文')
print("課程:%s ,學(xué)時(shí): %d" %(record.title,record.period))
#更新這行數(shù)據(jù)的period字段,保存
record.period = 200
record.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ù)。

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論