我基本上希望将数据帧写入SQL对象表,并将其外键映射到另一个表的主键,并使用下面的代码执行此操作。但是,我在行sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0)
中收到错误connection.execute(yc_node_hist.__table__.insert(),fk_yc_update = listofindices3[i],curve_date = row1['Date'], tenor_years = row1['Maturity'], yield_pct = row1['Yield_pct'])
。我在下面的代码之前在其他表中执行session.add_all()
序列。
#Retrieving the primary key for yc_update to set as foreign key for yc_node_hist
session.flush()
session.commit()
session = Session(bind=engine, expire_on_commit=False)
listofindices3 = []
connection = engine.connect()
trans = connection.begin()
#Function to write a dataframe into SQL
def writingtosql_yc_node_hist(df):
i = 0
for (i1, row1), (i2, row2) in pairwise(df.iterrows()):
# take first item from row_iterator
if row1['Currency'] == row2['Currency']:
r1 = connection.execute(yc_node_hist.__table__.select())
connection.execute(yc_node_hist.__table__.insert(),fk_yc_update = listofindices3[i],curve_date = row1['Date'], tenor_years = row1['Maturity'], yield_pct = row1['Yield_pct'])
else:
i = i + 1
r1 = connection.execute(yc_node_hist.__table__.select())
connection.execute(yc_node_hist.__table__.insert(),fk_yc_update = listofindices3[i],curve_date = row1['Date'], tenor_years = row1['Maturity'], yield_pct = row1['Yield_pct'])
trans.commit()
writingtosql_yc_node_hist(combined_datafinal)
trans.close()