dipl0
dipl0

Reputation: 1077

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()

Upvotes: 1

Views: 246

Answers (2)

Akshit Raj Singh
Akshit Raj Singh

Reputation: 11

This problem will only happen with a single input without a comma. It has got something to do with parsing as a tuple rather than the input string. For more than one input, it will be parsed normally.

Upvotes: 1

eatmeimadanish
eatmeimadanish

Reputation: 3907

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])

Upvotes: 1

Related Questions