m0_58184957 2021-10-22 17:03 采纳率: 33.3%
浏览 18
已结题

运行爬虫程序,将数据存储mysql,未报错,数据库查询无数据


class SunproPipeline:
    connect = None
    cursor = None
    def open_spider(self,spider):
        self.connect = pymysql.connect(host='127.0.0.1', port=3306, user='root', db='reptile', charset='utf8')
        self.cursor = self.connect.cursor()
    def process_item(self, item, spider):
        if item.__class__.__name__ == 'SunproItem':
            title_id = item['title_id']
            title_name = item['title_name']
            # print(title_id)
            # print(title_name)

            try:
                print('正在存储',title_id)
                print('正在存储',title_name)
                self.cursor.execute(f'insert into sun (id,name) value("{title_id}","{title_name}");')
                self.connect.commit()
                print('存储成功')
            except Exception as e:
                print(e)
                self.connect.rollback()
        else:
            new_id =item['new_id']
            new_cont =item['new_cont']
            # print(new_id)
            # print(new_cont)
            try:
                print('正在存储',new_id)
                print('正在存储',new_cont)
                self.cursor.execute(f'update  sun set cont="{new_cont}" where id="{new_id}";')
                self.connect.commit()
                print('存储成功')
            except Exception as e:
                print(e)
                self.connect.rollback()
        return item
    def close_spider(self,spider):
        print('*'*50,'程序结束')
        self.connect.close()

在sql语句与commit上下打印语句,都正常执行了,但数据库中无数据,求解

  • 写回答

1条回答 默认 最新

  • python收藏家 2021-10-22 17:13
    关注

    可以打印execute执行sql之后的返回值看看
    类似 rows = cur.execute(sql,("alex", "555")) # #执行sql语句,返回sql查询成功的记录数目
    print(rows)

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月8日
  • 创建了问题 10月22日

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题