您好,登錄后才能下訂單哦!
數(shù)據(jù)庫(kù)引擎
pymysql 取而代之 ----推薦
2的話pip install pymysql
3的話 pip3 install pymysql
安裝完成后用pip3 freeze 查看一下安裝的什么版本之類的
MySQLdb python3不支持,也TM很久沒(méi)更新了
sudo yum install python-devel mysql-devel
MySQL-python-1.2.5,這才是python2.7.6的mysql接口
https://pypi.python.org/pypi/MySQL-python/1.2.5
import MySQLdb
mysqlclient django1.9推薦,python3.3+支持
sudo yum install python-devel mysql-devel
pip install mysqlclient
MySQL Connector/Python
$ python manage.py makemigrations polls stored as amigration. $ python manage.py sqlmigrate polls 0001 just returns their SQL $ python manage.py check # this checks for any problems in your project without making migrations or touching the database. $ python manage.py migrate apply those changes to the database. 會(huì)自動(dòng)生成主鍵_id
python manage.py shell
>>> from books.models import Publisher >>> p1 = Publisher(name='Apress', address='....) >>> p1.save() >>> publisher_list = Publisher.objects.all() >>> publisher_list [<Publisher: Publisher object>]
`` objects.create()`` 一步完成對(duì)象的創(chuàng)建與存儲(chǔ)至數(shù)據(jù)庫(kù),就不需要save
>>> p1 = Publisher.objects.create(name='Apress', ... address='2855 Telegraph Avenue',..............
Publisher 對(duì)象添加一個(gè)方法 __unicode__() 。
from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) def __unicode__(self): return self.name
>>> from books.models import Publisher >>> publisher_list = Publisher.objects.all() >>> publisher_list [<Publisher: Apress>, <Publisher: O'Reilly>]
因?yàn)?span id="wrywhiw" class="Apple-converted-space"> Publisher 模型有一個(gè)自動(dòng)增加的主鍵 id ,所以第一次調(diào)用 save() 還多做了一件事: 計(jì)算這個(gè)主鍵的值并把它賦值給這個(gè)對(duì)象實(shí)例:
>>> p.id 52 # this will differ based on your own data
接下來(lái)再調(diào)用 save() 將不會(huì)創(chuàng)建新的記錄,而只是修改記錄內(nèi)容(也就是 執(zhí)行 UPDATE SQL語(yǔ)句,而不是INSERT 語(yǔ)句):
>>> p.name = 'Apress Publishing' >>> p.save()
注意,并不是只更新修改過(guò)的那個(gè)字段,所有的字段都會(huì)被更新。 這個(gè)操作有可能引起競(jìng)態(tài)條件
取出所有記錄:
>>> Publisher.objects.all() [<Publisher: Apress>, <Publisher: O'Reilly>]
數(shù)據(jù)過(guò)濾
filter()返回集合
你可以傳遞多個(gè)參數(shù)到 filter() 來(lái)縮小選取范圍:
>>> Publisher.objects.filter(country="U.S.A.", state_province="CA") [<Publisher: Apress>]
在 name 和 contains 之間有雙下劃線。contains部分會(huì)被Django翻譯成LIKE語(yǔ)句:
>>> Publisher.objects.filter(name__contains="press") [<Publisher: Apress>]
注意,SQL缺省的 = 操作符是精確匹配的
SELECT id, name, address, city, state_province, country, website FROM books_publisher WHERE name LIKE '%press%';
其他的一些查找類型有:icontains(大小寫無(wú)關(guān)的LIKE),startswith和endswith, 還有range(SQLBETWEEN查詢)。 附錄C詳細(xì)描述了所有的查找類型。
`` get()`` 返回單個(gè)的對(duì)象
>>> Publisher.objects.get(name="Apress") <Publisher: Apress>
如果結(jié)果是多個(gè)對(duì)象,會(huì)導(dǎo)致拋出異常,如果查詢沒(méi)有返回結(jié)果也會(huì)拋出異常:
DoesNotExist: Publisher matching query does not exist.
Publisher.DoesNotExist 在你的應(yīng)用中,你可以捕獲并處理這個(gè)異常,像這樣:
try: p = Publisher.objects.get(name='Apress') except Publisher.DoesNotExist: print "Apress isn't in the database yet." else: print "Apress is in the database."
有序返回查詢結(jié)果
如果需要以多個(gè)字段為標(biāo)準(zhǔn)進(jìn)行排序(第二個(gè)字段會(huì)在第一個(gè)字段的值相同的情況下被使用到),減號(hào) -
表示逆向排序:
>>> Publisher.objects.order_by("-state_province", "address") [<Publisher: Apress>, <Publisher: O'Reilly>]
Django讓你可以指定模型的缺省排序方式: class Meta,內(nèi)嵌類。
class Publisher(models.Model): name = models.CharField(max_length=30) def __unicode__(self): return self.name **class Meta:** **ordering = ['name']**
“鏈?zhǔn)健钡男问剑?/p>
>>> Publisher.objects.filter(country="U.S.A.").order_by("-name") [<Publisher: O'Reilly>, <Publisher: Apress>]
限制返回的數(shù)據(jù)
取出固定數(shù)目的記錄,剪語(yǔ)句,不支持負(fù)索引,但可逆序查詢
>>> Publisher.objects.order_by('-name')[0:2]
更新多個(gè)對(duì)象
更新一條記錄
>>> Publisher.objects.filter(id=52).update(name='Apress Publishing')
更新多條記錄。
>>> Publisher.objects.all().update(country='USA') # 所有Publisher的country字段 2 # 表示受影響的記錄條數(shù)
注:save()方法,這個(gè)方法會(huì)更新一行里的所有列。 而某些情況下,我們只需要更新行里的某幾列。
刪除數(shù)據(jù)庫(kù)中的對(duì)象只需調(diào)用該對(duì)象的delete()方法即可:
>>> p = Publisher.objects.get(name="O'Reilly") >>> p.delete() >>> Publisher.objects.all() [<Publisher: Apress Publishing>]
>>> Publisher.objects.filter(country='USA').delete() # 刪除部分 >>> Publisher.objects.all().delete() # 刪除所有
為了預(yù)防誤刪除掉某一個(gè)表內(nèi)的所有數(shù)據(jù),Django要求在刪除表內(nèi)所有數(shù)據(jù)時(shí)顯示使用all()。 否則報(bào)錯(cuò)
>>> Publisher.objects.delete() # 必需加all() Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Manager' object has no attribute 'delete'
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。