Python 使用SQLAlchemy指定pyODBC选项(特别是fast\u executemany=True)

Python 使用SQLAlchemy指定pyODBC选项(特别是fast\u executemany=True),python,sqlalchemy,pyodbc,Python,Sqlalchemy,Pyodbc,我想在使用SQLAlchemy向表中插入行时,为pyODBC驱动程序打开fast_executemany选项。默认情况下,它是,代码运行非常慢。。。有人能建议怎么做吗 编辑: 我使用的是pyODBC 4.0.21和SQLAlchemy 1.1.13,下面是我使用的代码的简化示例 import sqlalchemy as sa def InsertIntoDB(self, tablename, colnames, data, create = False): """ Insert

我想在使用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)
试试这个

crsr = cnxn.cursor()
crsr.fast_executemany = True

从1.3版开始,SQLAlchemy直接支持


您正在使用哪个版本的pyodbc?pyodbc-4.0.21;SQLAlchemy-1.1.13然后我应该使用
crsr.executemany()
来执行我的语句吗?谢谢,它成功了!虽然有点苦乐参半。原则上,没有必要使用SQLAlchemy…:)是的,没错。这是pyofbc而不是sqlalchemy。
engine = create_engine(connection_uri, fast_executemany=True)