处理pyformat参数失败; %s“%错误

时间:2014-07-24 21:13:55

标签: mysql python-2.7

我在脚本中有以下查询:

    conn = mysql.connector.connect(**config)
    connect = conn.cursor()
    params = {'build': self.tc.tag, 'page': self, 'object_id': self.object_id, 'page_header': self.page_header,
              'interval': t.interval, 'timestamp': timestamp}
    query = u'INSERT INTO page_load_times (build, page, object_id, page_header, elapsed_time, date_run) ' \
            'VALUES (%(build)s, %(page)s, %(object_id)s, %(page_header)s, %(interval)s, %(timestamp)s)'
    connect.execute(query, params)
    conn.commit()
    conn.close()

当我运行它时,它会从'page': self参数中获得以下错误:

"Failed processing pyformat-parameters; %s" % err)
ProgrammingError: Failed processing pyformat-parameters; Python 'appmainmenu' 
cannot be converted to a MySQL type

self仅返回一个unicode字符串。我将'charset': 'utf8', 'use_unicode': True添加到我的配置中以进行连接,这也没有帮助。

1 个答案:

答案 0 :(得分:2)

所以在params词典中我将self转换为unicode,所以现在它看起来像unicode(self)