Search code examples
pythonpostgresqlpsycopg2

TypeError: not all arguments converted during string formatting - psycopg2


THe following query appears to fail to execute with the error - TypeError: not all arguments converted during string formatting. Where am I going wrong here?

    cursor = connection.cursor()

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

Solution

  • You need to add a comma to your input tuple.

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

    Or you could do this:

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