您当前的位置: 首页 >  爬虫

奇巧小软件

暂无认证

  • 3浏览

    0关注

    16博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

6.爬虫:存储数据

奇巧小软件 发布时间:2022-08-26 15:33:37 ,浏览量:3

peewee:一款轻量级的ORM框架

使用ORM有很多好处,其中之一是:隔离数据库与数据库版本之间的差异

sqlalchemy相对完善

peewee相对轻量级,文档质量高。

官方文档:http://docs.peewee-orm.com/en/latest/

1.如何定义表和生成表:
from peewee import *db = MySQLDatabase('test',host='127.0.0.1',port=3306,user='root',password='root')class Person(Model):    name = CharField(max_length=20)    birthday = DateField()    class Meta:        database = db # This model uses the "people.db" database.if __name__ == '__main__':    db.create_tables([Person])

(官方文档)Field与数据库类型的对应关系 alt

2.通过peewee对数据进行增、删、改、查
# 增uncle_bob = Person(name='bob', birthday=date(1960, 2, 15))uncle_bob.save()
# 查# get方法只获取一条数据,在取不到数据时会抛出异常,取出来的是Person类型Bob = Person.select().where(Person.name == 'Bob').get()  #方式1egg = Person.get(Person.name == 'egg') #方式2print(egg.birthday)----------------------------------------------------------------------# 批量查询,取不到数据不会抛异常for person in Person.select():  # 支持切片Person.select()[:3]    print(person.name,person.birthday)
# 改person.birthday = date(1989,11,30)person.save()  # save()可以新建,也可以更新# 删person.delete_instance()

本文由 mdnice 多平台发布

关注
打赏
1661325404
查看更多评论
立即登录/注册

微信扫码登录

0.0410s