Python MySQL Select With a Filter
In this page:
sql = "SELECT * FROM table_name WHERE column = %s"
cursor.execute(sql, (value,))
rows = cursor.fetchall()
Basic WHERE Filtering
cursor.execute("SELECT * FROM customers WHERE address = %s", ("Park Lane 38",)) table की हर row की बजाय सिर्फ वे rows return करता है जहाँ address column बिल्कुल दी गई value से मेल खाता है।
उदाहरण: Basic WHERE Filtering
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers WHERE address = %s", ("Park Lane 38",)) # only exact matches returned
print("Filtered by exact address match")
LIKE से Pattern Matching
% wildcard character के साथ LIKE, किसी WHERE clause के अंदर partial text matching enable करता है -- 'Ap%' Ap से शुरू होने वाली किसी भी चीज़ से मेल खाता है, '%way%' कहीं भी way रखने वाली किसी भी चीज़ से मेल खाता है, जो search-style features के लिए उपयोगी है।
उदाहरण: Pattern Matching with LIKE
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers WHERE address LIKE %s", ("%way%",)) # % wildcard matches anywhere in the value
print("Matches any address containing 'way'")
AND / OR से Conditions मिलाना
एक ही WHERE clause में AND (सभी conditions सही होनी चाहिए) या OR (कम से कम एक सही होनी चाहिए) इस्तेमाल करके कई conditions मिलाई जा सकती हैं, ठीक वैसे ही जैसे Python के अपने and/or operators boolean expressions को मिलाते हैं।
उदाहरण: Combining Conditions with AND / OR
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM customers WHERE city = %s AND age > %s", # AND requires both conditions to hold
("Austin", 18),
)
print("Both conditions must be true")
NULL Values पर Filter करना
IS NULL और IS NOT NULL ही किसी column में कोई value न होने (या होने) के आधार पर rows filter करने के एकमात्र सही तरीके हैं -- NULL के खिलाफ एक plain = comparison SQL में कभी true return नहीं करता, चाहे NULL की तुलना खुद NULL से ही क्यों न की जाए।
उदाहरण: Filtering on NULL Values
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers WHERE address IS NULL") # = NULL would never match
print("= NULL never matches; IS NULL is required")
Parameterized Queries क्यों मायने रखती हैं
किसी WHERE clause को f-string या % formatting से user input को सीधे SQL string में डालकर बनाना attacker को उस input के ज़रिए अपना खुद का SQL logic inject करने देता है, यह एक vulnerability है जिसे SQL injection कहते हैं; parameterized %s placeholders data और SQL structure को सख्ती से अलग रखकर इसे पूरी तरह रोकते हैं।
उदाहरण: Why Parameterized Queries Matter
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
user_input = "Park Lane 38"
# Safe: value is passed separately, never concatenated into the SQL string
cursor.execute("SELECT * FROM customers WHERE address = %s", (user_input,))
print("Protected against SQL injection")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join