Python MySQL Where Clause (original) (raw)
Last Updated : 25 Apr, 2025
Where clause is used in MySQL database to filter the data as per the condition required. You can fetch, delete or update a particular set of data in MySQL database by using where clause.
Syntax
SELECT column1, column2, …. columnN FROM [TABLE NAME] WHERE [CONDITION];
The above syntax is used for displaying a certain set of data following the condition.
Example: Consider the following database named college and having a table name as a student.
Schema of the database:
Database:
Where Clause In Python
Steps to use where clause in Python is:
- First form a connection between MySQL and Python program. It is done by importing mysql.connector package and using mysql.connector.connect() method, for passing the user name, password, host (optional default: localhost) and, database (optional) as parameters to it.
- Now, create a cursor object on the connection object created above by using cursor() method. A database cursor is a control structure that enables traversal over the records in a database.
- Then, execute the where clause statement by passing it through execute() method.
Python3
import
mysql.connector
conn
=
mysql.connector.connect(user
=
'your_username'
,
`` host
=
'localhost'
,
`` password
=
'your_password'
,
`` database
=
'College'
)
mycursor
=
conn.cursor();
sql
=
"select
*
from
Student where Roll_no >
=
3
;"
mycursor.execute(sql)
myresult
=
mycursor.fetchall()
for
x
in
myresult:
`` print
(x)
conn.close()
OUTPUT:
Similar Reads
- Python MySQL Python MySQL Connector is a Python driver that helps to integrate Python and MySQL. This Python MySQL library allows the conversion between Python and MySQL data types. MySQL Connector API is implemented using pure Python and does not require any third-party library. This Python MySQL tutorial will 9 min read
- How to install MySQL Connector Package in Python MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. A connector is employed when we have to use MySQL with other pro 2 min read
- Connect MySQL database using MySQL-Connector Python While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.MySQL Connector module of Python is used to connect MySQL databases with 3 min read
- How to Install MySQLdb module for Python in Linux? In this article, we are discussing how to connect to the MySQL database module for python in Linux. MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API. Installing MySQLdb module for Python o 2 min read