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

时间:2019-04-23 22:10:23

标签: python postgresql psycopg2

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

    cursor = connection.cursor()

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

1 个答案:

答案 0 :(得分: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])