In the example below there is two dataframes, df and df1. I also have a MySQL database with two tables. Tables1 and Table2, both has the same columns as df.
What I want to do is:
- Use df1 to locate the correct row in table1,
- Move the row to table2 and update the columns 'REMOVED' (From False to True) and 'REMOVED_DATE (from None to '2022-01-10).
- Remove the row from table1
Below you see my effort. But it dosen't work, and I can imagine there is a better solution available. Any help would be much appreciated.
df=pd.DataFrame({'ID':[25, 10],
'DATE':['2021-11-13', '2021-12-03'],
'LOC': ['NY', 'ML'],
'MAIL':['[email protected]', '[email protected]'],
'NR': ['100000', '200000'],
'REMOVED': [False, False],
'REMOVED_DATE':[None,None]})
df1=pd.DataFrame({'ID':[25],
'REMOVED':[True],
'REMOVED_DATE':['2022-01-11']})
cols = "`,`".join([str(i) for i in df.columns.tolist()]) #use df to mirror the columns in the tables in the database.
for i, row in df1.iterrows():
id=row['ID']
sql=f"SELECT ID FROM table1 WHERE id={id}" # Check if ID is in table1.
cursor.execute(sql)
z=cursor.fetchall()
if not z:
print('insert') # if not, move on.
else: #If ID in, update, move and delete
sql=f"""UPDATE table1 SET (`" +cols + "`) VALUES (" + "%s,"*(len(row)-1) + "%s) WHERE id={id}""" # Update the whole row where ID match.
cursor.execute(sql, tuple(row))
mydb.commit()
sql=f"""INSERT INTO table2 SELECT * FROM table1 WHERE id={id}""" #insert it into table2
cursor.execute(str(sql), tuple(row))
mydb.commit()
sql=f"""DELETE from table1 WHERE id={id}""" # Delete it from table1
cursor.execute(sql)
mydb.commit()
cursor.execute()for theUPDATEstatement.