Federico Leoni
Federico Leoni

Reputation: 111

psycopg2: use a list in a query without index rows

I'm writing to a db using this query with various placeholders:

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

Is there any optimized solution like

cur.execute(sql_write_ord, ([order, newTable]))

without generating an 'IndexError: list index out of range'?

Upvotes: 1

Views: 189

Answers (1)

alecxe
alecxe

Reputation: 474131

You can just concatenate the lists:

cur.execute(sql_write_ord, [order] + newTable)

Upvotes: 1

Related Questions