A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-mysql-update-query/ below:

Python MySQL - Update Query

Python MySQL - Update Query

Last Updated : 30 Jun, 2025

The UPDATE query in SQL is used to modify existing records in a table. It allows you to update specific columns' values in one or more rows of a table. It's important to note that the UPDATE query affects only the data, not the structure of the table.

Syntax

UPDATE tablename
SET column1 = "new_value", column2 = "new_value"
WHERE condition;

DATABASE IN USE:

Example 1: Update the Age of a Student

In this example, we'll update the age of a student named "Rishi Kumar" in the STUDENT table. We will change his age to 23:

Python
import mysql.connector

# Connecting to the Database
mydb = mysql.connector.connect(
  host="localhost",
  database="College",
  user="root",  # Replace with your MySQL username
  password="your_password"  # Replace with your MySQL password
)

# Create a cursor object
cs = mydb.cursor()

# SQL query to update the age of a student
statement = "UPDATE STUDENT SET AGE = 23 WHERE Name = 'Rishi Kumar'"

# Execute the update statement
cs.execute(statement)

# Commit the changes to the database
mydb.commit()

# Disconnecting from the database
mydb.close()

Output:

MySQL Update

Explanation:

Example 2: Correcting the Spelling of a Student's Name

In this example, we will correct the spelling of the student's name from "SK Anirban" to "S.K. Anirban" in the STUDENT table:

Python
import mysql.connector

# Connecting to the Database
mydb = mysql.connector.connect(
  host="localhost",
  database="College",
  user="root",  
  password="your_password"  # Replace with your MySQL password
)

# Create a cursor object
cs = mydb.cursor()

# SQL query to update the name of the student
statement = "UPDATE STUDENT SET Name = 'S.K. Anirban' WHERE Name = 'SK Anirban'"

# Execute the update statement
cs.execute(statement)

# Commit the changes to the database
mydb.commit()

# Disconnecting from the database
mydb.close()

Output:

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4