06數(shù)據(jù)存儲(chǔ)peewee
數(shù)據(jù)存儲(chǔ)
一、MySQL
1.1 pymysql
- 建立數(shù)據(jù)庫(kù)連接db =
pymysql.connect(...)- 參數(shù)host:連接的mysql主機(jī),如果本機(jī)是'127.0.0.1'
- 參數(shù)port:連接的mysql主機(jī)的端口,默認(rèn)是3306
- 參數(shù)database:數(shù)據(jù)庫(kù)的名稱
- 參數(shù)user:連接的用戶名
- 參數(shù)password:連接的密碼
- 參數(shù)charset:通信采用的編碼方式,推薦使用utf8
- 創(chuàng)建游標(biāo)對(duì)象cur = db.cursor()
- 游標(biāo)方法: cur.execute("insert ....")
- 提交到數(shù)據(jù)庫(kù)或者獲取數(shù)據(jù) : db.commit()
- 關(guān)閉游標(biāo)對(duì)象 :cur.close()
- 斷開(kāi)數(shù)據(jù)庫(kù)連接 :db.close()
1.2 peewee
幫助我們之前了解下后面Django去操作數(shù)據(jù)庫(kù)
peewee是Python編程語(yǔ)言下的一款ORM框架。O是object,也就是對(duì)象的意思,R是relation,翻譯成中文是關(guān)系,也就是關(guān)系數(shù)據(jù)庫(kù)中數(shù)據(jù)表的意思,M是mapping,是映射的意思。在ORM框架中,它幫我們把類和數(shù)據(jù)表進(jìn)行了一個(gè)映射,可以讓我們通過(guò)類和類對(duì)象就能操作它所對(duì)應(yīng)的表中的數(shù)據(jù)。ORM框架還有一個(gè)功能,它可以根據(jù)我們?cè)O(shè)計(jì)的類自動(dòng)幫我們生成數(shù)據(jù)庫(kù)中的表,省去了我們自己建表的過(guò)程。
安裝:pip install peewee
from peewee import *
db = MySQLDatabase(
"spider",
host="127.0.0.1",
port=3306,
user="root",
password="123456"
)
class Person(Model):
name = CharField(max_length=20)
birthday = DateField(null=True)
class Meta:
database = db # This model uses the "people.db" database.
db.create_tables([Person])
| 字段類型 | MySQL |
|---|---|
BigIntegerField |
bigint |
IntegerField |
int |
SmallIntegerField |
smallint |
FloatField |
Float |
DoubleField |
Double |
DecimalField |
Decimal |
CharField |
varchar |
FixedCharField |
char |
TextField |
text |
BlobField |
blob |
DateTimeField |
DateTime |
DateField |
Date |
TimeField |
Time |
二、Excel
python內(nèi)置模塊中是沒(méi)有提供處理Excel文件的模塊,想要在python中操作Excel是需要安裝第三方模塊openpyxl,這個(gè)模塊中集成了python操作Excel的相關(guān)功能。

浙公網(wǎng)安備 33010602011771號(hào)