首页 > 其他 > 详细

爬虫-数据存储(8)

时间:2020-07-17 23:24:06      阅读:72      评论:0      收藏:0      [点我收藏+]

Python的orm数据存储有三大类型:

pymysl,sqlachemy,peewee

安装:

pip install pymysql【解决peewee的驱动依赖问题】

pip install peewee

peewee的具体实现如下:

from peewee import *

db = MySQLDatabase("spider", 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.
        table_name = "users"#修改表名字,一般默认为person。小写的类名

#数据的增、删、改、查

if __name__ == "__main__":
    db.create_tables([Person])
  #这个列表传入的参数将表示产生的表

peewee的增删改查

from datetime import date

    #生成数据
    # uncle_bob = Person(name=‘Bob‘, birthday=date(1960, 1, 15))
    # uncle_bob.save()  # bob is now stored in the database
    #

    #查询数据(只获取一条) get方法在取不到数据会抛出异常
    # bobby = Person.select().where(Person.name == ‘Bob‘).get()
    # print(bobby.birthday)
    # a = 1
    # bobby = Person.get(Person.name == ‘bobb‘)
    # print(bobby.birthday)
    #query是modelselect对象 可以当做list来操作 __getitem__
    query = Person.select().where(Person.name == Bob)
    for person in query:
        person.delete_instance()
        # person.birthday = date(1960, 1, 17)
        # person.save() #在没有数据存在的时候新增数据,存在的时候修改数据

 

爬虫-数据存储(8)

原文:https://www.cnblogs.com/topass123/p/13332811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!