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


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


In this article we will learn how to dalete value from 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 delete value from table using python

First we need to create one file named delete.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 = "DELETE FROM student WHERE location = 'Mumbai'"

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 delete.py



After executing the above command record has been deleted from the table.

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


mysql> select * from student;
How to delete value from table of mysql database using python on CentOS and RHEL 6/7  step by step guide
How to delete value from 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

1 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