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


How to fetch 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 fetch value 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 fetch value from table using python

First we need to create one file named fetch.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()

 

cur.execute("select * from student")

result = cur.fetchall()

for x in result:
     print(x)

 How to execute the python progam

To execute the python program we need to execute the below command.
[root@localhost ~]# python3 fetch.py




output:
[root@localhost project]# python3 fetch.py
('Rahul', 'Pune')
How to fetch value from table of mysql database using python on CentOS and RHEL 6/7  step by step guide
How to fetch value from table of mysql database using python on CentOS and RHEL 6/7  step by step guide

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

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


mysql> select * from student;

mysql> select * from student;
+-------+----------+
| name  | location |
+-------+----------+
| Rahul | Pune     |
+-------+----------+
1 row in set (0.00 sec)


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




Please Donate To Bitcoin Address: [[address]]

Donation of [[value]] BTC Received. Thank You.
[[error]]
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