In python, you can use cursor.execute ()
to update existing records in a MySQL database table using the sql UPDATE statement.
Syntax
cursor.execute("UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition")
Update User Email
import mysql.connector # Establish connection to MySQL server connection = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" ) cursor = connection.cursor() # Update a specific record in the 'users' table update_query = "UPDATE users SET email = 'new_email@example.com' WHERE id = 1" cursor.execute(update_query) # Commit the changes connection.commit() # Verify the update cursor.execute("SELECT * FROM users WHERE id = 1") updated_user = cursor.fetchone() print("Updated User:", updated_user) connection.close()
In this example, An UPDATE query is executed to change the email address of the user with id = 1
in the users table. The connection.commit()
method is called to save the changes permanently.