使用SQLAlchemy指定pyODBC选项(特别是fast_executemany = True)

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

我想在使用SQLAlchemy向表中插入行时打开pyODBC驱动程序的fast_executemany选项。默认情况下它是和代码运行真的很慢......任何人都可以建议如何做到这一点?

编辑:

我正在使用pyODBC 4.0.21和SQLAlchemy 1.1.13,下面将介绍我正在使用的代码的简化示例。

import sqlalchemy as sa

def InsertIntoDB(self, tablename, colnames, data, create = False):
    """
    Inserts data into given db table
    Args:
    tablename - name of db table with dbname
    colnames - column names to insert to
    data - a list of tuples, a tuple per row
    """

    # reflect table into a sqlalchemy object
    meta = sa.MetaData(bind=self.engine)
    reflected_table = sa.Table(tablename, meta, autoload=True)

    # prepare an input object for sa.connection.execute
    execute_inp = []
    for i in data:
        execute_inp.append(dict(zip(colnames, i)))

    # Insert values
    self.connection.execute(reflected_table.insert(),execute_inp)
python sqlalchemy pyodbc
1个回答
1
投票

尝试使用pyodbc

crsr = cnxn.cursor()
crsr.fast_executemany = True
© www.soinside.com 2019 - 2024. All rights reserved.