Following is the code in python that updates the records in the required database tables. Is there a better way to handle the same?
Read in SO that scanning dataframe row by row is a time consuming process. What is the better way to handle the same?
for index, row in outputData.iterrows():
try:
updatesql = " update table set [fieldname] = {0:f} where dt = \'{1:s}\'" .format(fieldvalue , currentdt)
updatecursor.execute(updatesql)
sql_conn.commit();
except IOError as e:
print ("({})".format(e))
pass
except (RuntimeError, TypeError, NameError) as e:
print ("({})".format(e))
pass
Based on the discussion below, made the changes but facing two problems.
updatesql = " update table set [fieldname] = ? where dt = ?"
data = (outputData.reindex( ['fieldvalue'], currentDt,axis='columns').to_numpy())
# EXECUTE QUERY AND BIND LIST OF TUPLES
updatecursor.executemany(updatesql, data.tolist())
sql_conn.commit()
Problems a) Date is constant and not part of the OutputData dataframe. b) Float values are stored in scientific format. Prefer float values to be stored with precisions.