且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

类型错误:参数 1 必须是字符串或 unicode 对象

更新时间:2021-12-05 22:24:34

该日志显示了一个 元组 unicode 字符串,而不是一个 unicode 字符串.在执行检查之前尝试记录 query 的类型.

That log is showing a tuple of unicode strings, rather than a unicode string. Try logging the type of query before you execute it to check.

记录参数化查询的首选方式是:

The preferred way to log parameterised query is:

query = u"UPDATE posts SET translated_text='%s', detected_language='%s' WHERE post_id=%s;"
vars = translation, detected_language['language'], str(post_id) # tuple
cur.execute(query, vars)

您可能还希望将 vars 的字符串显式转换为 unicode,而不是依赖于隐式完成.例如.

You may also wish to explicitly convert the strings of vars into unicode rather than relying on this to be done implicitly. eg.

vars = translation, detected_language['language'].decode("utf8"), str(post_id).decode("utf8")