Python - Update Records in MySQL Data Database - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we’ll show see how to update records in MySQL Database using Python step by step.
To set up the environment, we need to connect MySQL with Python, go through this tutorial to see how to connect MySQL using mysql connector.
If you follow along with the tutorial link, we have connected everything and have code below in our IDE.
# Importing the MySQL-Python-connector
import mysql.connector as mysqlConnector
# Creating connection with the MySQL Server Running 
conn = mysqlConnector.connect(host='localhost',user='root',passwd='root')
# Checking if connection is made or not
if conn:
    print("Connection Successful :)")
else:
    print("Connection Failed :(")
# Creating a cursor object to traverse the resultset
cur = conn.cursor()
cur.execute("SHOW DATABASES")
for row in cur:
    print(row)
# Closing the connection
conn.close()
Output:
Connection Successful 
(‘information_schema’,)
(‘mysql’,)
(‘performance_schema’,)
(‘sys’,)
Use-case – A student having std_id =10 has name shubham, not shivam as in the database and we have to update that using SQL queries in Python. We will fix that using the UPDATE <table-name>  SQL Query. This will set the name ‘shubham’ where std_id=10. Now because we have made modifications, it is important to commit the changes to reflect in the database permanently.
import mysql.connector as mysqlConnector
conn = mysqlConnector.connect(host='localhost',user='root',passwd='root',database='cse')
if conn:print("Connection Successful :)")
else:print("Connection Failed :(")
cur = conn.cursor()
try:
    cur.execute("update students set name='shubham' where std_id=10")
    print("Query Executed Successfully !!!")
    conn.commit()
except Exception as e:
    print("Invalid Query")
    print(e)
conn.close()
Output:
Connection Successful :)
Query Executed Successfully !!!
How to connect MySQL db
Read Data from Database
MySQL connector
Happy Learning 🙂

Python Insert data into MySQL Database
Python read data from MySQL Database
Python MySQL create database and table
How to connect MySQL DB with Python
How to connect MySQL Database in Eclipse
Python Set Data Structure in Depth
Python Tuple Data Structure in Depth
Python List Data Structure In Depth
What are different Python Data Types
JDBC Update Program Example
Python – How to merge two dict in Python ?
Python raw_input read input from keyboard
How to Remove Spaces from String in Python
Different ways to use Lambdas in Python
How to access for loop index in Python

Python Insert data into MySQL Database
Python read data from MySQL Database
Python MySQL create database and table
How to connect MySQL DB with Python
How to connect MySQL Database in Eclipse
Python Set Data Structure in Depth
Python Tuple Data Structure in Depth
Python List Data Structure In Depth
What are different Python Data Types
JDBC Update Program Example
Python – How to merge two dict in Python ?
Python raw_input read input from keyboard
How to Remove Spaces from String in Python
Different ways to use Lambdas in Python
How to access for loop index in Python
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list