Python Program to fetch all values from a table using MySQL


In this program, you will learn how to fetch all values from a table using Python and MySQL.


create database xiith;

use xiith;

create table emp(id varchar(10), name varchar(30),salary int(10))

select * from emp;

Example: How to fetch all values from a table using Python and MySQL

import mysql.connector

myconn = mysql.connector.connect(host="localhost", user="root", passwd="12345678", database="xiith")
cur = myconn.cursor()

try:
    cur.execute("select * from emp")
    result = cur.fetchall()

    for value in result:
        print(value)

    myconn.commit()
except:
    myconn.rollback()
myconn.close()

Output:

('101', 'John', 40000)
('102', 'Mike', 40000)

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.