0

I'm trying to pass multiple values to my INSERT function using psycopg2. According to the documentation, you must past a list or tuple, which I am doing, but it's still breaking with the following error:

"File "c:\action.py", line 42, in putTicket cur.execute(SQL, ins) TypeError: not all arguments converted during string formatting"

Code:

data = response.json()                  # get JSON response

record = data[0]                    # header row
col = record.keys()                 # column names
a = []

def putTicket(conn):
    cur = conn.cursor()

    for record in data:                     # data rows
        a = []
        y = record.values()                 # values on this row in an array

        for col in y:
            a.append(col)

    ins = tuple(a)

    SQL = "INSERT INTO fd_tickets VALUES (%s)"
    cur.execute(SQL, ins)


print("Using psycopg2...")

myConnection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database, port=port)
putTicket(myConnection)
myConnection.close()   
2
  • Did you try this: cur.execute("insert into fd_tickets values %s", [ins]) ? (You should really state the column names in that insert statement.) Commented Mar 28, 2018 at 13:07
  • This solved it. And I agree, but does it matter if I'm inserting whole rows every time? The DBs are extremely unlikely to change. Commented Mar 28, 2018 at 13:18

1 Answer 1

0

You have multiple values, but only one placeholder. You need as many placeholders as there are values in your tuple.

placeholders = ','.join(['%s'] * len(a))
SQL = "INSERT INTO fd_tickets VALUES ({})".format(placeholders)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.