自动提交导致熊猫to_sql失败

时间:2019-05-29 17:32:08

标签: python-3.x pandas sqlalchemy

我的引擎参数有问题:

    import pyodbc
    import pandas as pd
    from sqlalchemy import create_engine
    import urllib


    conn_str = (
        r'Driver=ODBC Driver 11 for SQL Server;'
        r'Server=Saturn;'
        r'Database=ExperienceRating2019;'
        r'Trusted_Connection=yes;'
    )
    quoted_conn_str = urllib.parse.quote_plus(conn_str)
    engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted_conn_str)).execution_options(autocommit=True)
    cnxn = engine.connect()

    splitpoint = 17000
    excel_file = "#2 DRATIO RUN.xlsx"
    d_ratio_sheet = "D RATIO & ELR"

    d_ratio = pd.read_excel(open(excel_file,'rb'),sheet_name = d_ratio_sheet)
    d_ratio.to_sql("d_ratio", cnxn, if_exists = 'replace')

我将收到以下错误:

DBAPIError:(pyodbc.Error)('HY010','[HY010] [Microsoft] [SQL Server的ODBC驱动程序11]功能序列错误(0)(SQLFetch)')(此错误的背景位于:{{ 3}})

如果我更改引擎以丢失自动提交

    engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted_conn_str))

错误消失了(是!),但是稍后在我执行SP的代码中,它将不再提交:

    engine.execute("sp_refresh_inputs")

**问题:**如何更改我的连接以使pandas和sqlalchemy都能正常工作?

1 个答案:

答案 0 :(得分:0)

我最终使用了两种引擎,一种用于熊猫,另一种用于sqlalchemy:

.column