Update Operation
The UPDATE statement is used to modify existing records in a table. This example demonstrates updating data using Python.
Example: Updating Data in the 'students' Table
import mysql.connector
# Establishing a connection
connection = mysql.connector.connect(
host='localhost',
user='root',
password='yourpassword',
database='student_db'
)
cursor = connection.cursor()
# Updating data in the 'students' table
cursor.execute("UPDATE students SET grade = 'B+' WHERE name = 'Alice'")
connection.commit()
print("Data updated successfully")
cursor.execute("SELECT * FROM students")
cursor.close()
connection.close()
Output
Data updated successfully
Database Changed.
id | name | age | grade |
---|---|---|---|
1 | Alice | 20 | B+ |
2 | Bob | 22 | B |
3 | Charlie | 21 | A |