TypeError:不是在字符串格式化期间转换的所有参数 - psycopg2

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

以下查询似乎无法执行错误 - TypeError:并非在字符串格式化期间转换所有参数。我在哪里错了?

    cursor = connection.cursor()

    cursor.execute("INSERT INTO darkweb (onionurl, sitetext) VALUES(%s, 'test') ON CONFLICT (onionurl) DO NOTHING)", (onion))
    connection.commit() 
    cursor.close()
python postgresql psycopg2
1个回答
1
投票

您需要在输入元组中添加逗号。

cursor = connection.cursor()

cursor.execute("INSERT INTO darkweb (onionurl, sitetext) VALUES(%s, 'test') ON CONFLICT (onionurl) DO NOTHING)", (onion,))
connection.commit() 
cursor.close()

或者你可以这样做:

cursor.execute("INSERT INTO darkweb (onionurl, sitetext) VALUES(%s, 'test') ON CONFLICT (onionurl) DO NOTHING)", [onion])
© www.soinside.com 2019 - 2024. All rights reserved.