手把手带你入门Python爬虫(四、ORM与peewee)
生活随笔
收集整理的這篇文章主要介紹了
手把手带你入门Python爬虫(四、ORM与peewee)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ORM與peewee
- 一、為什么要用ORM
- 二、ORM的選擇
- 三、peewee使用
- 1. 安裝
- 2. 創建并使用
- 3. 增刪查改
- (1) 新增
- (2) 查詢數據
- (3) 修改數據
- (4) 刪除數據
一、為什么要用ORM
- 隔離數據庫和數據庫版本之間的差異
- 便于維護
- ORM會提供防sql注入等功能
- 變量傳遞式的調用更加簡單
- ORM越來越流行
二、ORM的選擇
| peewee | Django式的API,使其易用 輕量實現,很容易和任意web框架集成 | 不支持自動化 schema 遷移 多對多查詢寫起來不直觀 |
| SQLObject | 采用了易懂的ActiveRecord 模式 ;一個相對較小的代碼庫 | 方法和類的命名遵循了Java 的小駝峰風格 ;不支持數據庫session隔離工作單元 |
| Storm | 清爽輕量的API,短學習曲線和長期可維護性;不需要特殊的類構造函數,也沒有必要的基類 | 迫使程序員手工寫表格創建的DDL語句,而不是從模型類自動派生;Storm的貢獻者必須把他們的貢獻的版權給Canonical公司 |
| Django’s ORM | 易用,學習曲線短;和Django緊密集合,用Django時使用約定俗成的方法去操作數據庫 | 不好處理復雜的查詢,強制開發者回到原生SQL;緊密和Django集成,使得在Django環境外很難使用 |
| SQLAlchemy | 企業級 API,使得代碼有健壯性和適應性;靈活的設計,使得能輕松寫復雜查詢 | 工作單元概念不常見;重量級 API,導致長學習曲線 |
我們選擇peewee這個框架來學習,因為它簡單、靈活、申明方式和django的ORM接近。且star數量高,活躍度高,文檔質量高。
官方文檔:http://docs.peewee-orm.com/en/latest/
三、peewee使用
1. 安裝
切換到虛擬環境,然后安裝
pip install peewee2. 創建并使用
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]) # 根據模型創建數據表生成的數據表,表名默認為類名,默認會加一個ID字段(主鍵):
Field types table(數據庫與模型字段對應表)
| 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]) # 創建數據表from datetime import date# 生成數據bob = Person(name="Bob", birthday=date(2020, 12, 12))# 新增數據到數據庫bob.save()(2) 查詢數據
if __name__ == "__main__":# 只查詢一條數據 get方法在取不到數據會拋出異常,需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# 查詢多條數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:print(b.name)print(b.birthday)(3) 修改數據
if __name__ == "__main__":from datetime import date# 修改數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.birthday = date(1997, 10, 16)b.save() # 在沒有數據的時候新增,存在的時候修改(4) 刪除數據
if __name__ == "__main__":# 刪除數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.delete_instance()總結
以上是生活随笔為你收集整理的手把手带你入门Python爬虫(四、ORM与peewee)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: testng查看覆盖率_使用Cobert
- 下一篇: thinkPHP 阿里云OSS 上传文件