← Back to Python Course | Chapter 15: Python & MySQL | Lesson 6 of 12

Python MySQL Select With a Filter

SELECT statement में जोड़ा गया एक WHERE clause rows को सिर्फ उन तक filter करता है जो किसी दी गई condition से मेल खाती हैं -- बिल्कुल उसी तरह जैसे if statement यह filter करता है कि code की कौन सी branch चले, बस इसे database server खुद, कोई भी data Python को वापस भेजने से पहले evaluate करता है।
Syntax
python
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 से मेल खाता है।

Note: Comparison value को हमेशा parameterized tuple argument के ज़रिए पास करें, कभी SQL string में सीधे embed न करें, उन values के लिए भी जो हानिरहित लगें।
Warning: ("value",) जैसे single-value tuple में trailing comma भूल जाना इसे tuple की बजाय plain string बना देता है, जो एक confusing error raise करता है।

उदाहरण: Basic WHERE Filtering

python
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 के लिए उपयोगी है।

Note: एक basic case-insensitive substring search feature implement करने के लिए leading और trailing % (जैसे "%term%") के साथ LIKE इस्तेमाल करें।
Warning: Leading % (जैसे "%way") वाला LIKE pattern किसी standard index का efficiently इस्तेमाल नहीं कर सकता, जो बहुत बड़ी tables पर searches को काफी धीमा कर सकता है।

उदाहरण: Pattern Matching with LIKE

python
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 को मिलाते हैं।

Note: एक ही WHERE clause में AND और OR दोनों मिलाते समय यह स्पष्ट करने के लिए parentheses इस्तेमाल करें कि कौन सी conditions साथ group होती हैं, ताकि ambiguity न रहे।
Warning: MySQL by default AND को OR से ज़्यादा precedence देता है, बिल्कुल Python की तरह -- बिना grouped किए दोनों को मिलाना इरादे से अलग तरीके से filter कर सकता है।

उदाहरण: Combining Conditions with AND / OR

python
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 से ही क्यों न की जाए।

Note: याद रखें कि SQL में, NULL का मतलब "unknown" है, "empty" नहीं -- यही वजह है कि इसके खिलाफ standard equality comparisons उम्मीद के मुताबिक कभी काम नहीं करते।
Warning: WHERE column = NULL एक silent logical bug है, कोई syntax error नहीं -- यह बस हमेशा zero rows return करता है, जिसे debug करना confusing हो सकता है।

उदाहरण: Filtering on NULL Values

python
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 को सख्ती से अलग रखकर इसे पूरी तरह रोकते हैं।

Note: अपने खुद के code से बाहर से आने वाली हर single value (user input, कोई API response, कोई file) को untrusted मानें, और उसे हमेशा एक parameterized placeholder से पास करें, कभी string concatenation से नहीं।
Warning: वे values भी जो स्पष्ट रूप से safe लगती हैं, जैसे किसी URL से आने वाली numeric ID, अब भी parameterized placeholder से गुज़रनी चाहिए -- consistency उस एक exception को रोकती है जो असली vulnerability बन जाती है।

उदाहरण: Why Parameterized Queries Matter

python
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")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.