python - psycopg2:在没有索引行的查询中使用列表

标签 python postgresql list python-2.7 psycopg2

我正在使用带有各种占位符的查询写入数据库:

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/

相关文章:

python - 是否有另一种方法可以用白色填充旋转图像外部的区域? 'fillcolor' 不适用于旧版本的 Python

python - numpy.unique 抛出错误

javascript - Selenium 蟒 : How to Scroll Down in a Pop Up window

python - 如何在 Python 中使用正则表达式提取某些字符之间的所有子串?

postgresql - PQescapeLiteral 未定义?

Python:将数据从 CSV 导入 Postgres 时出错(错误:整数输入语法无效:)

json - 如果存在则从 json 返回特定值,如果不存在则返回零行

list - 反转列表时出现意外结果

java - 尝试从 <String> 数组列表中删除罗马数字和数字

html - 如何制作这样的下拉菜单(里面有列表的 div)?