How to update value into table of mysql database using python on CentOS and RHEL 6/7 step by step guide


How to update value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide


In this article we will learn how to update value into table using python language.

We need to install the mysql.connector module using the below command.

#pip3 install mysql.connector

Please follow the below link to insert value into table using python language.

https://techrideradmin.blogspot.com/2019/06/how-to-insert-value-into-table-of-mysql-using-python-on-centos-rhel-step-by-step-guide.html

How to update value into table using python

First we need to create one file named update.py and enter the below code into this file  .
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="abc123",
  database="pythonclass"
)

cur = mydb.cursor()

query = "UPDATE student SET location = 'Mumbai' WHERE location = 'Pune'"

cur.execute(query)

mydb.commit()

for affect the row mydb.commit() method is required.

How to execute the python progam

To execute the python program we need to execute the below command.

[root@localhost ~]# python3 update.py


After executing the above command record has been updated into  the table.

Now enter the mysql promt and verify it is updated  or not using “select” statement.


mysql> select * from student;
How to update value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide
How to update value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide


That’s all. If this article is helpful please share it!!!!




SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

2 comments:

  1. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles Python Programming Training

    ReplyDelete