TypeError:并非在psycopg2中的字符串格式化期间转换的所有参数

问题描述 投票:0回答:1

当我使用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个回答
1
投票

[VALUES需要括在方括号中的值的列表:

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

不要忘记提交事务。

© www.soinside.com 2019 - 2024. All rights reserved.