Python MySQL – Update Query

A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

Update Clause

The update is used to change the existing values in a database. By using update a specific value can be corrected or updated. It only affects the data and not the structure of the table.
The basic advantage provided by this command is that it keeps the table accurate.

Syntax:

UPDATE tablename
SET ="new value"
WHERE ="old value";

The following programs will help you understand this better.
DATABASE IN USE:

python-mysql-update



Example 1: Program to update the age of student named Rishi Kumar.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# update clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# drop clause
statement ="UPDATE STUDENT SET AGE = 23 WHERE Name ='Rishi Kumar'"
  
cs.execute(statement)
mydb.commit()
  
# Disconnecting from the database
mydb.close()

chevron_right


Output:

python-mysql-update1

Example 2: Program to correct the spelling of an Student named SK

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# update clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# drop clause
statement ="UPDATE STUDENT SET Name = 'S.K. Anirban' WHERE Name ='SK Anirban'"
  
cs.execute(statement)
mydb.commit()
  
# Disconnecting from the database
mydb.close()

chevron_right


Output:

python-mysql-update2




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.