In this Python tutorial, we will see how to select data in mysql database using select with where through python.
WHERE clause is used with SELECT, UPDATE, DELETE staements to perform operation based on certain conditions.
WHERE clause helps to filter the result based on the applied conditions.
Syntax for WHERE
SELECT * FROM table_name
WHERE Condition;
Now, let' see how to perform SELECT with WHERE clause in xampp server through python.
Before that make sure that you have created a database named - tutorialsinhand and table inside it market with the following records.
.
Steps:
1. Import mysql.connector module
Syntax:
import mysql.connector
2. Create the connection object to connect with xampp server using the below credentials.
Syntax:
connection_obj = mysql.connector.connect(
host="localhost",
user="root",
password=""
)
3. Create cursor object to execute the query.
Syntax:
connection_obj.cursor()
4. Write sql query to select columns by applying where clause from the table.
Syntax:
SELECT * FROM table_name where condition;
5. Fetch the resulted rows using fetchall().
Syntax:
cursor.fetchall()
6. Display using for loop
Syntax:
for i in results:
print(i)
Note - It will return the values in a tuple.
Example 1:
In this example, we are selecting all columns from market table with rating greater than 6.
So the condition applied is
SELECT * FROM market WHERE rating>6
With Python Code:
#import the module
import mysql.connector
#create the connection by specifying the
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="tutorialsinhand"
)
#creating cursor object
cursor_obj = connection_obj.cursor()
#select all columns from table with rating greater than 6
cursor_obj.execute("SELECT * FROM market WHERE rating>6")
#fetch the rows
results = cursor_obj.fetchall()
#get the values
for i in results:
print(i)
Output:
We can see that rows are selected where rating is greater than 6.
('koti-cente', 'kakumanu', 8)
('hali', 'hyderabad', 7)
Example 2:
In this example, we are selecting all columns from market table where market_name equal to "bakery"
So the condition applied is
SELECT * FROM market WHERE market_name="bakery"
With Python:
#import the module
import mysql.connector
#create the connection by specifying the
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="tutorialsinhand"
)
#creating cursor object
cursor_obj = connection_obj.cursor()
#select all columns from table where market_name is bakery
cursor_obj.execute("SELECT * FROM market WHERE market_name='bakery'")
#fetch the rows
results = cursor_obj.fetchall()
#get the values
for i in results:
print(i)
Output:
row with market_name as bakery is filtered.
('bakery', 'hyderabad', 5)
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Sep 17,2022