SQL Table:
Pandas Dataframe:
I am trying to write a python script that updates the Gender and Mark of N1, N3, N5 inside the SQL table based on their values inside the pandas dataframe without changing the Age field.
import pyodbc
df = pd.read_excel('edited_dataframe.xlsx')
try:
connection = pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};'
'Server=************;'
'Database=MyDB;'
'Trusted_Connection=Yes;'
)
cursor = connection.cursor()
for each in range(len(df_sc_cleaned)):
sql_update_query = "UPDATE mytable SET Mark=df['Mark'][each] , Gender=df['Gender'][each] if Name=df['Name'][each]"
cursor.execute(sql_update_query)
connection.commit()
if connection:
connection.close()
print("MySQL connection is closed")
The sql_update_query is an example of the logic I'm thinking of.