我正在使用带有各种占位符的查询写入数据库:
sql_write_ord = '''INSERT INTO rest_order_test (date_order, pos_line_id, pos_order_id, product_name, instructions, qty,
partner_name, status, to_wait, to_floor) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
order= '2016-06-10 16:36:53'
newTable = [12821, 4421, 'Crudo MD', 'Y', Decimal('1.0'), 'Mesa 10', 'A fazer', 'N', '0']
cur.execute(sql_write_ord,[order, newTable[0], newTable[1], newTable[2], newTable[3], newTable[4], newTable[5], newTable[6], newTable[7], newTable[8]])
有没有优化过的方案比如
cur.execute(sql_write_ord, ([order, newTable]))
没有生成“IndexError:列表索引超出范围”?
最佳答案
您可以只连接列表:
cur.execute(sql_write_ord, [order] + newTable)
关于python - psycopg2:在没有索引行的查询中使用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37755600/