【问题标题】:Python/MySQL query syntax causes TypeErrorPython/MySQL 查询语法导致 TypeError
【发布时间】:2016-01-04 07:56:11
【问题描述】:

在运行此代码段时,我得到TypeError('not all arguments converted during string formatting',)

write_query = "insert into %s_semantic (anchor,old_url,semantic_url) values (?,?,?)" %category
try:
    cursor.execute(write_query,(result[0],result[1],str(contextual_redirect)))
except Exception as e:
    print "DB write failed for %s beacuse %s" %(result[0],str(repr(e)))

上面的查询语法有什么问题?

【问题讨论】:

    标签: python mysql python-2.7 mysql-python


    【解决方案1】:

    MySQLdb 使用%s 作为占位符,而不是?

    由于您还在该 SQL 上进行字符串替换以插入表名,因此您需要将原始字符串中的 % 符号加倍。

    【讨论】:

    • "insert into %%s_semantic (anchor,old_url,semantic_url) values (?,?,?)" %category 是这样的吗?
    猜你喜欢
    • 2016-08-07
    • 2016-08-05
    • 2020-11-12
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2012-11-19
    相关资源
    最近更新 更多