为什么peewee无法保存数据呢?
来源:9-5 通过requests完成京东详情页数据的获取

慕先生8168270
2020-03-10
如下的代码,为什么最后的打印结果会是0呢?也就是没有保存成功的意思,希望老师能帮我一下,谢谢!
from peewee import *
db = MySQLDatabase('python_spider', host='localhost', port=3306, user='root', password='admin')
class TestGood(Model):
good_id = IntegerField(primary_key=True)
name = CharField()
price = IntegerField()
good_href = CharField() #商品链接
total_comment_count = IntegerField() #总评价数 default=0
good_rate = IntegerField() #好评率
good_count = IntegerField() #好评数
general_count = IntegerField() #中评数
poor_count = IntegerField() #差评数
after_count = IntegerField() #追评数
video_count = IntegerField() #视频评价数
class Meta:
database = db
def saveGood(int1,str2,int3,str4,int5,int6,int7,int8,int9,int10,int11):
myGood = TestGood(good_id=int1, name=str2, price=int3,
good_href=str4, total_comment_count=int5, good_rate=int6,
good_count=int7, general_count=int8, poor_count=int9,
after_count=int10, video_count=int11)
return myGood.save()
db.create_tables([TestGood])
print(saveGood(1,'sdff',3,'www.sdd.com',5,6,7,8,9,10,11))
写回答
1回答
-
bobby
2020-03-11
return myGood.save(force_insert=True)
添加这个参数, 因为你设置的good_id
是主键,但是这个主键你又赋值了,所以peewee认为你是要更新数据,但是这个id的值在数据库中并不存在,所以就更新不了 也不能新增force_insert=True这样设置代表你是要新增数据012020-03-11
相似问题