url_object_id无法插入到数据库
来源:4-19 pipeline数据库保存
慕妹406553
2020-08-17
为了更加的面向对象编程,同时防止在使用字典的时候键名出错问题。我试用peewee根据数据库生成如下models文件:
from peewee import *
database = MySQLDatabase('article_spider',
**{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'host': '127.0.0.1',
'port': 3306, 'user': 'root', 'password': 'root'})
class UnknownField(object):
def __init__(self, *_, **__): pass
class BaseModel(Model):
class Meta:
database = database
class CnblogsArticle(BaseModel):
comment_nums = IntegerField(constraints=[SQL("DEFAULT 0")])
content = TextField(null=True)
create_date = DateTimeField(null=True)
fav_nums = IntegerField(constraints=[SQL("DEFAULT 0")])
front_image_path = CharField(null=True)
front_image_url = CharField(null=True)
parise_nums = IntegerField(constraints=[SQL("DEFAULT 0")])
tags = CharField(null=True)
title = CharField()
url = CharField()
url_object_id = CharField(primary_key=True)
class Meta:
table_name = 'cnblogs_article'
MysqlPipeline类代码如下:
class MysqlPipeline(object):
def process_item(self, item, spider):
if CnblogsArticle.table_exists() == False:
CnblogsArticle.create_table()
data = CnblogsArticle()
data.title = item["title"]
data.content = item["content"]
data.url = item["url"]
#data.url_object_id = item["url_object_id"]
data.comment_nums = item["comment_nums"]
data.create_date = item["create_date"]
data.fav_nums = item["fav_nums"]
data.front_image_path = item["front_image_path"]
data.front_image_url = item["front_image_url"]
data.parise_nums = item["parise_nums"]
data.tags = item["tags"]
data.save()
return item
目前遇到的问题是 url_object_id无法插入到mysql中,将其注释掉可以顺利插入其他字段。经过调试url_object_id有值,就是无法插入到数据库。
盼指导解惑。谢谢!
写回答
1回答
-
慕妹406553
提问者
2020-08-17
经过测试,表必须设置id字段 为自增 主键
032020-08-22
相似问题