陳娜 付沛
摘要:Django通過(guò)Manager(模型管理器)提供數(shù)據(jù)庫(kù)訪問(wèn)接口。默認(rèn)情況下,Django為每個(gè)模型添加一個(gè)名為objects的管理器,調(diào)用objects的各種方法?可完成相關(guān)的數(shù)據(jù)庫(kù)操作;也可以用模型管理器的raw()方法執(zhí)行原始SQL查詢(xún)并返回模型示例;或者不使用模型直接執(zhí)行原始SQL。本文介紹3種方法,可根據(jù)自身情況選擇一種方法完成數(shù)據(jù)庫(kù)操作。
在django中我們只需要操作類(lèi)或者對(duì)象,ORM它系統(tǒng)幫你根據(jù)類(lèi)和對(duì)象來(lái)操作數(shù)據(jù)庫(kù)
類(lèi)--à數(shù)據(jù)表?對(duì)象?---〉數(shù)據(jù)行?屬性—〉?字段
首先創(chuàng)建項(xiàng)目mydjango
1、?創(chuàng)建book?應(yīng)用
2、?在settings.py文件中加入book應(yīng)用
3、?打開(kāi)models.py定義表的結(jié)構(gòu)
class?faqsdata(models.Model):
question?=?models.CharField(max_length=20,?blank=True)
answer?=?models.CharField(max_length=20,?blank=True)
4、使用命令生成遷移文件(系統(tǒng)幫你生成的sql語(yǔ)句)
python?manage.py?makemigrations
5、使用命令?運(yùn)行?遷移文件(相當(dāng)于執(zhí)行sql命令)
python?manage.py?migrate
6、打開(kāi)終端輸入以下命令進(jìn)入當(dāng)前項(xiàng)目的Python交互環(huán)境
python?manage.py?shell
7、導(dǎo)入模型類(lèi)
from?book.models?import?;faqsdata
一、使用默認(rèn)管理器objects
Django通過(guò)模型對(duì)象的默認(rèn)管理器objects提供了多種獲取數(shù)據(jù)的方法
(1)create方法創(chuàng)建模型的對(duì)象,并將數(shù)據(jù)保存進(jìn)數(shù)據(jù)庫(kù)
>>>?ds=faqsdata.objects.create(question='test',answer='bbb')
>>>?ds=faqsdata.objects.create(question='test434',answer='aaa')
(2)all方法獲取所有數(shù)據(jù)行,相當(dāng)于sql中的select?*?from?****
>>>?ds=faqsdata.objects.all()
>>>?for?a?in?ds:
...?print(a.id,a.question,a.answer)
...
1?test?bbb
2?test4334?aaa
(3)get、filter、update方法過(guò)濾出符合條件的行進(jìn)行更新
1更新單行
>>>?ds=faqsdata.objects.get(id="1")
>>>?print(ds.id,ds.question,ds.answer)
1?test?bbb
>>>?ds.question="yyyyyyyy"
>>>?d.save()
>>>?print(ds.id,ds.question,ds.answer)
1?yyyyyyyy?bbb
2更新多行
>>>?ds=faqsdata.objects.filter(id__lte="9")
>>>?ds.update(answer='bbbbbb')
4
>>>?ds=faqsdata.objects.update(answer='ppppp')?全表更新
>>>
(4)filter、delete方法過(guò)濾出符合條件的行進(jìn)行刪除
>>>?faqsdata.objects.filter(id__gt=9).delete()?get刪除單行
(3,?{'book.faqsdata':?3})
二、用模型管理器raw()方法執(zhí)行原始SQL
>>>?ds=scores.objects.raw("select?*?from?book_scores?where?yw<%s",[50])
>>>?for?a?in?ds:
...?print(a.id,a.kh,a.xm,a.yw,a.sx,a.bj)
...
>>>?ds=scores.objects.raw("update?book_scores?set?yw=100?where?id=3")
>>>?ds.query._execute_query()
>>>d=scores.objects.raw("insert?into?book_scores(kh,xm,yw,sx,bj)?values?('10110199','fdsaf',11,22,'class1')")
>>>?d.query._execute_query()
>>>?d=scores.objects.raw("delete?from?book_scores?where?id=63")
>>>?d.query._execute_query()
三、不使用模型訪問(wèn)數(shù)據(jù)庫(kù)
也可以不使用模型管理器直接訪問(wèn)數(shù)據(jù)庫(kù),使用游標(biāo),但是有SQL注入風(fēng)險(xiǎn),基本步驟如下:
調(diào)用django.db.connection.cursor()方法獲得一個(gè)游標(biāo)對(duì)象。django.db.connection對(duì)象代表默認(rèn)數(shù)據(jù)庫(kù)連接。
調(diào)用游標(biāo)對(duì)象的execute(sql)方法執(zhí)行SQL命令。
調(diào)用游標(biāo)對(duì)象的fetchall()或fetchone()方法返回?cái)?shù)據(jù)。
>>>?from?django.db?import?connection
>>>?cursor=connection.cursor()
>>>?cursor.execute("select?*?from?book_scores?where?id<7")
>>>?for?a?in?cursor.fetchall():
...?print(a[0],a[1],a[2])
...
>>>a=cursor.execute("insert?into?book_scores(kh,xm,yw,sx,bj)?values?('10110199','fdsaf',11,22,'class1')")
>>>?a=cursor.execute("update?book_scores?set?yw=100?where?id=10")
>>>?a=cursor.execute("delete?from?book_scores?where?id=10")