Python read data from MySQL Database - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we’ll show how to read data from MySQL DB Python step by step.
To set up the environment, we need to connect MySQL with Python, go through this tutorial to see how to connect MySQL using mysql connector.
If you follow along with the tutorial link, we have connected everything and have code below in our IDE.
# Importing the MySQL-Python-connector
import mysql.connector as mysqlConnector
# Creating connection with the MySQL Server Running 
conn = mysqlConnector.connect(host='localhost',user='root',passwd='root')
# Checking if connection is made or not
if conn:
    print("Connection Successful :)")
else:
    print("Connection Failed :(")
# Creating a cursor object to traverse the resultset
cur = conn.cursor()
cur.execute("SHOW DATABASES")
for row in cur:
    print(row)
# Closing the connection
conn.close()
Output:
Connection Successful 
(‘information_schema’,)
(‘mysql’,)
(‘performance_schema’,)
(‘sys’,)
We can access the data using the SQL Select query which will return the result set according to the conditions specified. Let us access data from our table.
import mysql.connector as mysqlConnector
conn = mysqlConnector.connect(host='localhost',user='root',passwd='root',database='cse')
if conn:print("Connection Successful :)")
else:print("Connection Failed :(")
cur = conn.cursor()
try:
    cur.execute("select * from students")
    print("Query Executed Successfully !!!")
    for row in cur:
        print(row)
except Exception as e:
    print("Invalid Query")
    print(e)
conn.close()
Output:
Connection Successful :)
Query Executed Successfully !!!
(13, 'Kunal', 91001, 'a')
(1, 'rahul', 91567, 'b')
(10, 'shivam', 91223, 'a')
(31, 'shivam', 91085, 'c')
import mysql.connector as mysqlConnector
conn = mysqlConnector.connect(host='localhost',user='root',passwd='root',database='cse')
if conn:print("Connection Successful :)")
else:print("Connection Failed :(")
cur = conn.cursor()
try:
    cur.execute("select * from students where name='shivam'")
    print("Query Executed Successfully !!!")
    for row in cur:
        print(row)
except Exception as e:
    print("Invalid Query")
    print(e)
conn.close()
Output:
Connection Successful :)
Query Executed Successfully !!!
(10, 'shivam', 91223, 'a')
(31, 'shivam', 91085, 'c')
Now that we have successfully learned how to retrieve the data from the tables.
Python MySQL connector
Python MySQL PIP
Happy Learning 🙂

Python Insert data into MySQL Database
Python MySQL create database and table
Read an Image in JDBC Example
How to Remove Spaces from String in Python
How to Read CSV File in Python
How to connect MySQL DB with Python
Python How to read input from keyboard
How to read a text file in Python ?
How to read JSON file in Python ?
Python raw_input read input from keyboard
What are different Python Data Types
Python List Data Structure In Depth
Python Tuple Data Structure in Depth
How to connect MySQL Database in Eclipse
Spring Boot Security MySQL Database Integration Example

Python Insert data into MySQL Database
Python MySQL create database and table
Read an Image in JDBC Example
How to Remove Spaces from String in Python
How to Read CSV File in Python
How to connect MySQL DB with Python
Python How to read input from keyboard
How to read a text file in Python ?
How to read JSON file in Python ?
Python raw_input read input from keyboard
What are different Python Data Types
Python List Data Structure In Depth
Python Tuple Data Structure in Depth
How to connect MySQL Database in Eclipse
Spring Boot Security MySQL Database Integration Example
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list