Python Database MySQL Select Statement

MySQL Connection In Python

The Python MySQL Connector is required for Python to connect to the database.

PIP makes it easy to install the Mysql Connector.

The MySQL module must be imported first. This tutorial uses MariaDB 10.11.8 and Python 3.12.4 on Fedora Linux 40.

MySQL Interface

Sample MySQL functions
Name Description Example
connect Create database connection connect(hostname, dbuser, dbpass, dbname)
execute Perform database query execute(query)
rowcount Gets the number of rows in the result set rowcount
Name Description Example

Install MySQL Driver

pip install mysql-connector-python

Python MySQL Select Data

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT sid, name, age FROM Students")

result = mycursor.fetchall()

for x in result:
	print(x)

SQL Injection Security Warning

Variable input must be sanitized, validated, properly formatted and escaped. For this tutorial, the variable input is known and trusted and security is not concerning.

Python MySQL Select Data Escape String

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT sid, name, age FROM Students WHERE name = %s", ("D'Arcy", ))

result = mycursor.fetchall()

for x in result:
	print(x)

MySQL Database Table Structure
PhpMyAdmin Displaying MySQL Database Students Structure

MySQL Database Table Data
PhpMyAdmin Displaying MySQL Database Students Data

Python MySQL Select
Geany IDE Displayed Python MySQL Select Code

Python MySQL Select Result
Geany IDE Displayed Python MySQL Select Result


Usage

You can use any IDE or text editor and the command line to compile and execute Python code. In this tutorial, the OjamboShop.com Learning Python Course completion inspired the database connection.

Open Source

Python is licensed under the Python Software Foundation License. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Courses are optimized for your web browser on any device.

OjamboShop.com Learning Python Course
OjamboShop.com Learning Python Interactive Online Course

OjamboShop.com Learning PHP Course
OjamboShop.com Learning PHP Interactive Online Course

Limited Time Offer:

OjamboShop.com is offering 20% off coupon code SCHOOL for Learning Python Course or for Learning PHP Course until End Day 2024.

Learn Programming Ebooks:

Ebooks can be downloaded to your reader of choice.

OjamboShop.com Learning Python Ebook
OjamboShop.com Learning Python Ebook Cover Page

OjamboShop.com Learning PHP Ebook
OjamboShop.com Learning PHP Ebook With Sample Page

Conclusion:

Python makes it easy to perform database connections using the MySQL Connector. The MySQl module must be imported in order to perform MySQL functions including the procedure to select database data.

Take this opportunity to learn the Python or PHP programming language by making a one-time purchase at Learning Python Course or Learning PHP Course. A web browser is the only thing needed to learn Python or PHP in 2024 at your leisure. All the developer tools are provided right in your web browser.

If you prefer to download ebook versions for your reader then you may purchase at Learning Python Ebook or Learning PHP Ebook

References: