python里tuple的坑

错误原因

class MysqlManager(object):
    def __init__(self):
        self.host = MYSQL_HOST
        self.user = MYSQL_USER
        self.password = MYSQL_PASSWORD
        self.db = MYSQL_DB
        self.charset = 'utf8'

    def __connect(self):
        return MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db, charset=self.charset)
    
    def do_count(self, sql_str, sql_param):
        connect = self.__connect()
        cursor = connect.cursor()
        try:
            cursor.execute(sql_str, sql_param)
            connect.commit()
            return cursor.fetchone()
        except Exception, e:
            print e
            import traceback
            traceback.print_exc()
            # 发生错误时会滚
            connect.rollback()
        finally:
            # 关闭游标连接
            cursor.close()
            # 关闭数据库连接
            connect.close()

if __name__ == "__main__":

    mysql_manager = MysqlManager()
    sql = """
            SELECT  COUNT( * ) FROM `merchant_queue` WHERE sMerchantFullName = %s
            """
    params = (
        'JCPenney'
    )
    result = mysql_manager.do_count(sql, params)

结果老是报异常not all arguments converted during string formatting错误,fuck

为什么

打断点发现params居然是字符串

python里tuple的坑_第1张图片
image.png

解决方法

原来元组只有元素时候,要加逗号,所以这里的params应该这样

 params = (
        'JCPenney',
    )

然后就,好了。。。哇靠

你可能感兴趣的:(python里tuple的坑)