python sqlalchemy不执行查询?

python sqlalchemy不执行查询?,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我正在尝试使用sqlalchemy从文件执行sql查询 当我运行查询时,我得到的结果是它影响了x行数,但当我检查数据库时,它实际上并没有向表中插入任何内容 这是我目前的代码: def import_to_db(df, table_name): df.to_sql( table_name, con=engine, schema='staging', if_exists='replace', index=

我正在尝试使用sqlalchemy从文件执行sql查询

当我运行查询时,我得到的结果是它影响了x行数,但当我检查数据库时,它实际上并没有向表中插入任何内容

这是我目前的代码:

def import_to_db(df, table_name):
    df.to_sql(
        table_name, 
        con=engine, 
        schema='staging', 
        if_exists='replace', 
        index= False, 
        method= 'multi'
    )

    print('imported data to staging.{}'.format(table_name))

    with open('/home/kyle/projects/data_pipelines/ahc/sql/etl_{}.sql'.format(table_name)) as fp:
        etl = fp.read()

    result = engine.execute(etl)
    print('moved {} rows to public.{}'.format(result.rowcount, table_name))
当我手动运行.sql脚本时,它们工作正常。我甚至尝试过创建存储过程,但也没有成功。下面是我正在执行的一个sql文件的示例:

--Delete Id's in prod table that are in current staging table
DELETE
FROM public.table
WHERE key IN
    (SELECT key FROM staging.table);

--Insert new/old id's into prod table and do any cleaning
INSERT INTO 
public.table
SELECT columna, columnb, columnc
FROM staging.table;

找到了一个解决方案,尽管我不完全理解

我加上开始;在我的脚本顶部,并提交;在底部


这是可行的,但我的行数现在显示为-1,因此对日志记录没有多大帮助。

也许可以尝试
cnxn=engine.raw_connection();cnxn.autocommit=True;crsr=cnxn.cursor();crsr.execute(etl)
nope。不起作用。也相关:TL;来自dupe目标的灾难恢复:您的SQL文件不是以SQLA识别为DDL/DML的内容开头(而是以注释开头),因此它不会自动提交。谢谢您这么做。如果你想把它打成答案,我会打上记号的。