“”“
host = "localhost"
user = "postgres"
password = "Lall1739!@#"
port = "5432"
dbname = "snp500"
con_form = "host={0} user={1} password={2} port={3} dbname={4}".format(host, user, password, port, dbname)
con = psycopg2.connect(con_form)
cur = con.cursor()
components_name, components_prices, components_fetch_dates = fetch_investing_snp500_components_datas()
for component_name in components_name:
table_name = component_name
cur.execute(
query=sql.SQL("CREATE TABLE %s"),
vars=(sql.Identifier(table_name))
)“”“
TypeError:“标识符”对象不支持索引
使用Python字符串内插来构建查询字符串也不是一个好主意。
因此,我试图使用sql模块编写一个查询。
使用sql模块编写查询究竟有什么好处,为什么我会得到Type错误?
发布于 2022-01-01 15:42:54
注意,execute用于参数替换(.i.e )。字符串),而要替换标识符(即表名)。为此,请使用SQL.format():
cur.execute(
query=sql.SQL("CREATE TABLE {table}").format(
table=sql.Identifier(table_name),
),
)https://stackoverflow.com/questions/70549482
复制相似问题