Reputation: 43
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()
Upvotes: 0
Views: 282
Reputation: 599480
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)
Upvotes: 0