使用SQLalchemy为PostgreSQL删除双引号

问题描述 投票:2回答:1

我正在尝试将200个SAS XPT文件导入到PostgreSQL数据库中:

engine = create_engine('postgresql://user:pwd@server:5432/dbName')
for file in listdir(dataPath):
    name, ext = file.split('.', 1)
    with open(join(dataPath, file), 'rb') as f:
        xport.to_dataframe(f).to_sql(name, engine, schema='schemaName', if_exists='replace', index=False)
    print("Successfully wrote ", file, " to database.")

但是,生成的SQL对所有标识符都有双引号,例如:CREATE TABLE "Y2009"."ACQ_F" ("SEQN" FLOAT(53), "ACD010A" FLOAT(53));。问题是,如果使用引号创建列/表/模式,每次我需要查询它们时,我必须同时包含引号,同时use the exact capitalization

我想摆脱引号,而我自己也不能编写自定义SQL,因为这些文件每个都有非常不同的结构。

python python-3.x postgresql sqlalchemy postgresql-10
1个回答
2
投票

PostgreSQL要求引用大写的表/列名称(reference)。这就是引用SQLalchemy构造的SQL中的标识符的原因。要避免这种情况,请将数据框的列名转换为全部小写:

with open(join(dataPath, file), 'rb') as f:
     data = xport.to_dataframe(f)
     data.columns = map(str.lower, data.columns)
     data.to_sql(name.lower(), engine, schema='y2007')
© www.soinside.com 2019 - 2024. All rights reserved.