【问题标题】:TypeError: not all arguments converted during string formatting in psycopg2TypeError:在 psycopg2 中的字符串格式化期间并非所有参数都转换了
【发布时间】:2020-03-15 23:55:30
【问题描述】:

当我使用 psycopg2 运行以下代码时:

cur.execute(
    """INSERT INTO logmsg (msg_type, file, msg) VALUES %s;""",
    ["Error", str(file), str(sys.exc_info()[0])])

我收到以下错误:

TypeError:字符串格式化期间并非所有参数都转换

有人可以帮我解决这个问题吗?

【问题讨论】:

标签: python postgresql pycharm psycopg2


【解决方案1】:

VALUES 需要括号中的值列表:

cur.execute(
    """INSERT INTO logmsg (msg_type, file, msg) VALUES (%s, %s, %s);""",
    ["Error", str(file), str(sys.exc_info()[0])])

不要忘记提交事务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2021-08-19
    • 2015-03-30
    • 2020-10-16
    • 2022-01-10
    相关资源
    最近更新 更多