06数据存储peewee
数据存储
一、MySQL
1.1 pymysql
- 建立数据库连接db =
pymysql.connect(...)
- 参数host:连接的mysql主机,如果本机是'127.0.0.1'
- 参数port:连接的mysql主机的端口,默认是3306
- 参数database:数据库的名称
- 参数user:连接的用户名
- 参数password:连接的密码
- 参数charset:通信采用的编码方式,推荐使用utf8
- 创建游标对象cur = db.cursor()
- 游标方法: cur.execute("insert ....")
- 提交到数据库或者获取数据 : db.commit()
- 关闭游标对象 :cur.close()
- 断开数据库连接 :db.close()
1.2 peewee
帮助我们之前了解下后面Django去操作数据库
peewee是Python编程语言下的一款ORM
框架。O是object,也就是对象的意思,R是relation,翻译成中文是关系,也就是关系数据库中数据表的意思,M是mapping,是映射的意思。在ORM
框架中,它帮我们把类和数据表进行了一个映射,可以让我们通过类和类对象就能操作它所对应的表中的数据。ORM
框架还有一个功能,它可以根据我们设计的类自动帮我们生成数据库中的表,省去了我们自己建表的过程。
安装: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内置模块中是没有提供处理Excel文件的模块,想要在python中操作Excel是需要安装第三方模块openpyxl
,这个模块中集成了python操作Excel的相关功能。