Writing Update SQL query in Python to update data in MySql Database.
Update statement is used to update some of the fields of a table. Where clause is used to filter the set of records and update. Here we pass code and name as parameter to update query and update to database. Parameter ‘code’ will be taken by where clause and name will be updated to database.
#MySql – Python Update Statement
File: update_emp.py
from database_connection import *
mycursor=db_con.cursor()
query="update emp set name='vimal' where
code='a01'"
mycursor.execute(query)
db_con.commit()
#update data based on input from screen
pcode=raw_input("Enter Code")
pname=raw_input("Enter Name")
query="update emp set name=%s where
code=%s"
mycursor.execute(query,(pname,pcode))
db_con.commit()
db_con.close()
Result
No comments:
Post a Comment