Python 'list' cannot be converted to a MySQL type

本文介绍了一种有效的方法来解决将Python列表类型转换为MySQL数据库时遇到的问题。通过将列表元素转换为适合数据库的格式,确保了数据能够正确插入。文章详细展示了如何使用Python的execute方法和适当的数据准备步骤,避免了类型不匹配错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   def process_item(self, item, spider):
       #1.Python 'list' cannot be converted to a MySQL type
        # self.cur.execute("insert into info values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
        #                  (item['xh'],item['sgdm'],item['zqdm'],item['name'],item['wsfxr'],item['ssr'],
        #                   item['fxl'],item['wsfxl'],item['sgsx'],item['fxj'],item['syl'],item['zql']))

       #2.ok
        values  = [item['xh'][0], item['sgdm'][0], item['zqdm'][0], item['name'][0], item['wsfxr'][0], item['ssr'][0],item['fxl'][0],
                   item['wsfxl'][0], item['sgsx'][0], item['fxj'][0], item['syl'][0], item['zql'][0]]
        self.cur.execute("insert into info values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",values)
        self.conn.commit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值