手把手带你入门Python爬虫(四、ORM与peewee)
生活随笔
收集整理的這篇文章主要介紹了
手把手带你入门Python爬虫(四、ORM与peewee)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ORM與peewee
- 一、為什么要用ORM
- 二、ORM的選擇
- 三、peewee使用
- 1. 安裝
- 2. 創(chuàng)建并使用
- 3. 增刪查改
- (1) 新增
- (2) 查詢數(shù)據(jù)
- (3) 修改數(shù)據(jù)
- (4) 刪除數(shù)據(jù)
一、為什么要用ORM
- 隔離數(shù)據(jù)庫和數(shù)據(jù)庫版本之間的差異
- 便于維護(hù)
- ORM會(huì)提供防sql注入等功能
- 變量傳遞式的調(diào)用更加簡(jiǎn)單
- ORM越來越流行
二、ORM的選擇
| peewee | Django式的API,使其易用 輕量實(shí)現(xiàn),很容易和任意web框架集成 | 不支持自動(dòng)化 schema 遷移 多對(duì)多查詢寫起來不直觀 |
| SQLObject | 采用了易懂的ActiveRecord 模式 ;一個(gè)相對(duì)較小的代碼庫 | 方法和類的命名遵循了Java 的小駝峰風(fēng)格 ;不支持?jǐn)?shù)據(jù)庫session隔離工作單元 |
| Storm | 清爽輕量的API,短學(xué)習(xí)曲線和長(zhǎng)期可維護(hù)性;不需要特殊的類構(gòu)造函數(shù),也沒有必要的基類 | 迫使程序員手工寫表格創(chuàng)建的DDL語句,而不是從模型類自動(dòng)派生;Storm的貢獻(xiàn)者必須把他們的貢獻(xiàn)的版權(quán)給Canonical公司 |
| Django’s ORM | 易用,學(xué)習(xí)曲線短;和Django緊密集合,用Django時(shí)使用約定俗成的方法去操作數(shù)據(jù)庫 | 不好處理復(fù)雜的查詢,強(qiáng)制開發(fā)者回到原生SQL;緊密和Django集成,使得在Django環(huán)境外很難使用 |
| SQLAlchemy | 企業(yè)級(jí) API,使得代碼有健壯性和適應(yīng)性;靈活的設(shè)計(jì),使得能輕松寫復(fù)雜查詢 | 工作單元概念不常見;重量級(jí) API,導(dǎo)致長(zhǎng)學(xué)習(xí)曲線 |
我們選擇peewee這個(gè)框架來學(xué)習(xí),因?yàn)樗?jiǎn)單、靈活、申明方式和django的ORM接近。且star數(shù)量高,活躍度高,文檔質(zhì)量高。
官方文檔:http://docs.peewee-orm.com/en/latest/
三、peewee使用
1. 安裝
切換到虛擬環(huán)境,然后安裝
pip install peewee2. 創(chuàng)建并使用
from peewee import *db = MySQLDatabase("py_spider", host="localhost", port=3307, user="root", password="root")class Person(Model):name = CharField()birthday = DateField()class Meta:database = db # This model uses the "people.db" database.if __name__ == "__main__":db.create_tables([Person]) # 根據(jù)模型創(chuàng)建數(shù)據(jù)表生成的數(shù)據(jù)表,表名默認(rèn)為類名,默認(rèn)會(huì)加一個(gè)ID字段(主鍵):
Field types table(數(shù)據(jù)庫與模型字段對(duì)應(yīng)表)
| AutoField | integer | serial | integer |
| BigAutoField | integer | bigserial | bigint |
| IntegerField | integer | integer | integer |
| BigIntegerField | integer | bigint | bigint |
| SmallIntegerField | integer | smallint | smallint |
| IdentityField | not supported | int identity | not supported |
| FloatField | real | real | real |
| DoubleField | real | double precision | double precision |
| DecimalField | decimal | numeric | numeric |
| CharField | varchar | varchar | varchar |
| FixedCharField | char | char | char |
| TextField | text | text | text |
| BlobField | blob | bytea | blob |
| BitField | integer | bigint | bigint |
| BigBitField | blob | bytea | blob |
| UUIDField | text | uuid | varchar(40) |
| BinaryUUIDField | blob | bytea | varbinary(16) |
| DateTimeField | datetime | timestamp | datetime |
| DateField | date | date | date |
| TimeField | time | time | time |
| TimestampField | integer | integer | integer |
| IPField | integer | bigint | bigint |
| BooleanField | integer | boolean | bool |
| BareField | untyped | not supported | not supported |
| ForeignKeyField | integer | integer | integer |
3. 增刪查改
(1) 新增
if __name__ == "__main__":# db.create_tables([Person]) # 創(chuàng)建數(shù)據(jù)表from datetime import date# 生成數(shù)據(jù)bob = Person(name="Bob", birthday=date(2020, 12, 12))# 新增數(shù)據(jù)到數(shù)據(jù)庫bob.save()(2) 查詢數(shù)據(jù)
if __name__ == "__main__":# 只查詢一條數(shù)據(jù) get方法在取不到數(shù)據(jù)會(huì)拋出異常,需try catchBob = Person.select().where(Person.name == 'Bob').get()print(Bob.name) # Bobprint(Bob.birthday) # 2020-12-12# 同上Bob2 = Person.get(Person.name == 'Bob')print(Bob2.name) # Bobprint(Bob2.birthday) # 2020-12-12# 查詢多條數(shù)據(jù)Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:print(b.name)print(b.birthday)(3) 修改數(shù)據(jù)
if __name__ == "__main__":from datetime import date# 修改數(shù)據(jù)Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.birthday = date(1997, 10, 16)b.save() # 在沒有數(shù)據(jù)的時(shí)候新增,存在的時(shí)候修改(4) 刪除數(shù)據(jù)
if __name__ == "__main__":# 刪除數(shù)據(jù)Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.delete_instance()總結(jié)
以上是生活随笔為你收集整理的手把手带你入门Python爬虫(四、ORM与peewee)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: testng查看覆盖率_使用Cobert
- 下一篇: python小项目超级大脑抱香_超级大脑