Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 混合占位符、executemany和表名_Python_Executemany - Fatal编程技术网

Python 混合占位符、executemany和表名

Python 混合占位符、executemany和表名,python,executemany,Python,Executemany,我可以使用下面的代码迭代python对象,但是我希望能够使用占位符作为模式和表名,通常我使用{}.{}和.format()方法来实现这一点,但是如何将两者结合起来呢 cur.executemany("INSERT INTO schema.table_name (x,y,z) " "values (%s, %s, %s)", top_sample) 我不确定是什么问题。您可以像这样很好地使用格式: cur.executemany("INSERT I

我可以使用下面的代码迭代python对象,但是我希望能够使用占位符作为模式和表名,通常我使用
{}.{}
.format()
方法来实现这一点,但是如何将两者结合起来呢

cur.executemany("INSERT INTO schema.table_name (x,y,z) "
                        "values (%s, %s, %s)", top_sample)

我不确定是什么问题。您可以像这样很好地使用
格式

cur.executemany("INSERT INTO {}.{} (x,y,z) values (%s, %s, %s)".format('hello', 'world'), top_sample)

将表名替换为表名取决于您使用的python,您可以尝试使用
f-string

schema = "schema"
table_name = "table_name"

cur.executemany(f"INSERT INTO {schema}.{table_name} (x,y,z) values (%s, %s, %s)", top_sample)
检查

另一个选项是简单的
格式

cur.executemany("INSERT INTO {schema}.{table_name} (x,y,z) values (%s, %s, %s)".format(schema=schema, table_name=table_name), top_sample)
但我发现第一种选择更短更干净

cur.executemany("INSERT INTO {schema}.{table_name} (x,y,z) values (%s, %s, %s)".format(schema=schema, table_name=table_name), top_sample)