我正在尝试使用该pd.to_sql方法将数据框推送到 SQL 服务器中。langyuage-python 3.6,熊猫版pandas(0.25.1)
引擎变量
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s"%PARAM,pool_size = 20,max_overflow =10,pool_recycle=60,pool_timeout=30)
这是调用staging_standardisation()方法的主要脚本。
import pyodbc
from sqlalchemy import create_engine,event
import pandas as pd
from utils import engine
@event.listens_for(engine, 'before_cursor_execute')
def plugin_bef_cursor_execute(conn, cursor, statement, params, context,executemany):
if executemany:
cursor.fast_executemany = True # replace from execute many to fast_executemany.
cursor.commit()
table = "table_name"
column_name = ["Some column_names"] #selecting some columns names
column_obj = {"a":"A",
"b":"B",
"c":"C"
}
staging_standardisation(engine=engine, table_name=table, column_names=column_name, column_obj=column_obj)
staging_standardisation 方法
def staging_standardisation(engine, table_name, column_names, column_obj, **kwargs):
try:
query = "SELECT * FROM dbo.License_Search"
sql_df = pd.read_sql(query, engine)
df = sql_df[column_names]
df = df.rename(index=str, columns=column_obj)
try:
if 'authority_id' in column_obj.values():
pass
else:
df['authority_id'] =53
except KeyError:
df['authority_id'] = None
try:
df.to_sql(name=staging_table, con=engine, index=False, if_exists='append') # this line causing error.
except Exception as exe:
print(table_name, exe)
投掷错误
(pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
The server failed to resume the transaction. Desc:8c00000007.
(3971) (SQLEndTran); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The
transaction active in this session has been committed or aborted by another session. (3926)')
(Background on this error at: http://sqlalche.me/e/f405)