Bootstrap

python函数参数是数据库表名_在Python/Pandas/PostgreSQL中将表名作为函数参数传递

我试图在我的SQL查询中使用'new'安全方式调用table\u name(它可以很好地抵御SQL注入攻击),如下所示:http://initd.org/psycopg/docs/sql.html#module-psycopg2.sql

但是我不能让它与我的代码一起工作(表名是我函数的一个参数)。在import pandas as pd

import psycopg2 as pg

import pandas.io.sql as psql

from psycopg2 import sql

sql_query = sql.SQL("SELECT * FROM {} limit %d offset %d" % (table_name, chunk_size, offset)).format(sql.Identifier(table_name))

df = psql.read_sql_query(sql_query, connection)

有什么建议吗?在

更新(在建议的答案之后):

我试过了

^{pr2}$

然后打电话给他:df = pd.concat(import_table(pg.connect("dbname=my_db user=my_user"), 'table_name', 100000, 0))

但我得到一个错误:---> 30 qry = sql_ct.format(**dict(table=table_name)) % (chunk_size, offset)

31 df_piece = psql.read_sql_query(qry, connection)

32

IndexError: tuple index out of range

;