自 SQLAlchemy 1.3.0,于2019-03-04发布以来,
sqlalchemy现在支持
engine = create_engine(sqlalchemy_url, fast_executemany=True)
mssql+pyodbc
方言。即
不再需要定义函数并使用
@event.listens_for(engine, 'before_cursor_execute').
但是,当我尝试向 mssql 写一个简单的测试数据帧时,它会返回错误:
DBAPIError:(pyodbc.Error)(“ HY010”,“ [HY010] [Microsoft] [用于SQL Server的ODBC驱动程序17]功能序列错误(0)(SQLParamData)”) [SQL:INSERT INTO fast_executemany_test([Date],[A],[B],[C],[D])值(?,?,?,?,?)] [参数:((datetime.datetime(2018 ,1、3、0、0),2.0、1.0、1.0,'Joe'),(datetime.datetime(2018、1、4、0、0),2.0、1.0、2.0,'Joe'),(datetime .datetime(2018,1,5,0,0),2.0,3.0,1.0,'Pete'),(datetime.datetime(2018,1,6,0,0),2.0,1.0,5.0,'Mary' ))] (此错误的背景位于:http://sqlalche.me/e/dbapi)
我已经阅读了文档,但找不到我在做什么错。
import sqlalchemy
import pandas as pd
DataFrame contains datetime, float, float, float, string.
test_columns = ['Date', 'A', 'B', 'C', 'D']
test_data = [
[datetime(2018, 1, 3), 2.0, 1.0, 1.0, 'Joe'],
[datetime(2018, 1, 4), 2.0, 1.0, 2.0, 'Joe'],
[datetime(2018, 1, 5), 2.0, 3.0, 1.0, 'Pete'],
[datetime(2018, 1, 6), 2.0, 1.0, 5.0, 'Mary'],
]
I am establishing connection as:
sqlUrl='mssql+pyodbc://ID:PASSWORD' + 'SERVER_ADDRESS' + '/' + 'DBName' + '?driver=ODBC+Driver+17+for+SQL+Server'
sqlcon = sqlalchemy.create_engine(sqlUrl,fast_executemany=True)
if sqlcon:
test_data.to_sql('FastTable_test', sqlcon, if_exists='replace',index=False)
print('Successfully written!')
It creates the table but due to error does not write any data into it.