Unable to write result to Oracle Database from Python using cx_oracle library

156 views Asked by At

I am trying to write result into Oracle Database using executemany command. However, I am neither getting any error message nor the database table is getting updated.

Using same connection object and cursor I am able to connect to database, extract data and insert the data into new table. However, I am NOT able to update the result to a existing table

I am using Oracle 19c database , python 3.8.8 and cx_oracle 8.0.0.

Reading the file from Oracle database table 'bench_project_d'. To reproduce the error I have created a csv file and reading the data from csv file.

The data has 7 fields

  1. ROW_ID type: NUMBER(19,0),
  2. GROUP_ID type: NUMBER(19,0),
  3. PLANNED_UNITS type: NUMBER,
  4. IQR_PTU(Calculated on the fly),
  5. Q1_PTU (Calculated on the fly) ,
  6. Q2_PTU (Calculated on the fly) ,
  7. ANOMALY type: NUMBER

All the fields are having data except the new column "ANOMALY". In this field , all are null values. This is a field where we want to store the results.

While importing the data to python, we are taking first 6 feature, calculate the anomaly field and push the anomaly result to database

'''python

#connecting to Database
username="**********"
password="********"
tsn="*********"
conn = cx_Oracle.connect(username, password, tsn)
cur = conn.cursor()

global_df = pd.read_csv("project_data.csv")
   
    #Filtering the group having more than 3 projects
   
    grouped = global_df.groupby("GROUP_ID")
    filtered_df = grouped.filter(lambda x: x["GROUP_ID"].count()>3)
    filtered_df
   
    #Handling zero Interquartile Range
    x = filtered_df[filtered_df['IQR_PTU'] == 0]['GROUP_ID'].unique()
    for i in x:
        filtered_df.loc[filtered_df['GROUP_ID'] == i,'IQR_PTU'] = (filtered_df[filtered_df['GROUP_ID']==i]['PLANNED_UNITS'].quantile(0.95)) - (filtered_df[filtered_df['GROUP_ID']==i]['PLANNED_UNITS'].quantile(0.05))
       
    
    #Creating the output 'Anomaly' field
   
    filtered_df['ANOMALY'] =0
    filtered_df.loc[filtered_df['PLANNED_UNITS'] > (filtered_df['Q2_PTU']+(1.5*filtered_df['IQR_PTU'])),'ANOMALY']=1
    filtered_df.loc[filtered_df['PLANNED_UNITS'] < (filtered_df['Q1_PTU']-(1.5*filtered_df['IQR_PTU'])),'ANOMALY']=-1
   
    
    #Formating the Dataframe
   
        result_df = df.loc[:,['ROW_ID','GROUP_ID', 'ANOMALY']]
        result_df = result_df.round(2)
        result_df=result_df.astype('object')
        value_to_stre_db=result_df.values.tolist()
   
    #Pushing the result to Database table bench_project_d
   
        statement = 'update bench_project_d set GROUP_ID = :2, ANOMALY= :3 where ROW_ID = :1'
        cur.executemany(statement, value_to_stre_db)
        conn.commit()

EDIT 1:

I have tried to convert list of array to list of tuples and executed the same code again. But still no luck.

rows = [tuple(x) for x in value_to_stre_db]

#Pushing the result to Database table bench_project_d
   
statement = ''update bench_project_d set GROUP_ID = :2, ANOMALY= :3 where ROW_ID = :1''
cur.executemany(statement, rows)
conn.commit()
0

There are 0 answers