Connection busy with results for another hstmt error (SQLAlchemy)

I am basically looking for writing data to a SQL object table along with mapping its foreign key to the primary key of another table, and I do it using the code below. But, I am getting an error sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0)

in the line 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'])

. I am sequencing session.add_all()

into other tables up to the code below.

#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()

      

+3


source to share





All Articles