我试图使用psycopg2将多个值传递给我的INSERT函数。根据{{3}},您必须通过我正在执行的列表或元组,但它仍然存在以下错误:
"文件" c:\ action.py",第42行,在putTicket中 cur.execute(SQL,ins) TypeError:并非在字符串格式化期间转换所有参数"
代码:
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()
答案 0 :(得分:0)
您有多个值,但只有一个占位符。您需要尽可能多的占位符,因为元组中有值。
placeholders = ','.join(['%s'] * len(a))
SQL = "INSERT INTO fd_tickets VALUES ({})".format(placeholders)