mysql unsupported operand type(s) for %: 'tuple' and 'tuple'

在Ubuntu 14.04和Python 2.7环境下,尝试使用Python操作MySQL时遇到'unsupported operand type(s) for %: 'tuple' and 'tuple'错误。问题出现在尝试将爬取的B站用户信息存入数据库的代码中。通过对比发现,将字符串的单引号改为双引号后问题得到解决,但具体原因尚不清楚。

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

环境:ubuntu14.04,python2.7

这篇文章只是为了记录我碰到的一个mysql相关的小问题

学习爬虫,看到一个项目:https://github.com/airingursb/bilibili-report](https://github.com/airingursb/bilibili-report

看了下代码,就是爬取bili用户信息后保存到mysql中。因为想之后方便提取mysql里的信息,所以将mysql相关的代码修改,放到另一个模块。

其中添加数据的代码如下:

class Ch_mysql():
    def __init__(self):
        self.conn = pymysql.connect(host='127.0.0.1', port=3306, user='user',
                                    passwd='passwd', db=database, charset='utf8')

    def add(self, kwargs):
        mid = kwargs.get('mid', None)
        name = kwargs.get('name', None)
        sex = kwargs.get('sex', None)
        face = kwargs.get('face', None)
        coins = kwargs.get('coins', None)
        regtime = kwargs.get('regtime', None)
        spacesta = kwargs.get('spacesta', None)
        birthday = kwargs.get('birthday', None)
        place = kwargs.get('place', None)
        description = kwargs.get('description', None)
        article = kwargs.get('article', None)
        fans = kwargs.get('fans', None)
        friend = kwargs.get('friend', None)
        attention = kwargs.get('attention', None)
        sign = kwargs.get('sign', None)
        attentions = kwargs.get('attentions', None)
        level = kwargs.get('level', None)
        exp = kwargs.get('exp', None)

        sql = "INSERT INTO bilibili_user_info VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        try:
            cur = self.conn.cursor()
            cur.execute(sql, (mid, mid, name, sex, face, coins, regtime, spacesta, birthday, place, description,
                              article, fans, friend, attention, sign, str(attentions), level, exp))
            cur.close()
            # raise  # for test
            self.conn.commit()  # commit之后raise不会回滚了
        except Exception, e:
            print 'db Exception: ', e
            self.conn.rollback()
        finally:
            # cur.close()
            # self.conn.commit()
            self.conn.close()

这是改好的代码,之前代码中sql因为用的单引号,所以一直报错:unsupported operand type(s) for %: 'tuple' and 'tuple'

查了些文章说是python的问题,搞不清原因,后来照以前的代码意义对比,将单引号改为双引号后就好了

呃,总之还不清楚为什么这样,聊作记录而已

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值