Python MySQL Select From Table
In this page:
cursor.execute("SELECT column1, column2 FROM table_name")
rows = cursor.fetchall()
सभी Columns Select करना
cursor.execute("SELECT * FROM customers") के बाद cursor.fetchall(), customers table की हर row के हर column को tuples की एक list के रूप में retrieve करता है, हर row के लिए एक tuple, जिससे आपको Python में table की पूरी contents मिल जाती हैं।
उदाहरण: Selecting All Columns
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers")
cursor.fetchall.return_value = [(1, "John", "Highway 21")]
print(cursor.fetchall()) # every column of every row, as a list of tuples
एक Single Row के लिए fetchone() इस्तेमाल करना
cursor.fetchone(), result set से बस अगली single row लाता है (या अगर और rows नहीं हैं तो None), यह तब एक अच्छा choice है जब आप जानते हों या उम्मीद करते हों कि ठीक एक matching row है, जैसे किसी unique ID से user ढूँढना।
उदाहरण: Using fetchone() for a Single Row
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers WHERE id = %s", (1,))
cursor.fetchone.return_value = (1, "John", "Highway 21")
print(cursor.fetchone()) # just the next single row, or None
Cursor पर सीधे Iterate करना
execute() चलने के बाद cursor object खुद iterable होता है -- उस पर सीधे for statement से loop करना अंदर ही अंदर rows को एक-एक करके fetch करता है, जो बड़े result sets के लिए memory-efficient है क्योंकि यह सब कुछ एक साथ list में load करने से बचाता है।
उदाहरण: Iterating the Cursor Directly
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers")
cursor.__iter__.return_value = iter([(1, "John"), (2, "Amy")])
for row in cursor: # fetches rows one at a time instead of loading them all at once
print(row)
Results को Dictionaries के रूप में Access करना
cursor argument के रूप में mysql.connector.connect(..., dictionary=True) (या conn.cursor(dictionary=True)) हर fetched row को plain tuple की बजाय column name से keyed एक dict के रूप में देता है, जो positional tuple indexes याद रखने से अक्सर ज़्यादा readable होता है।
उदाहरण: Accessing Results as Dictionaries
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor(dictionary=True) # rows come back as dicts keyed by column name
cursor.execute("SELECT * FROM customers")
cursor.fetchall.return_value = [{"id": 1, "name": "John"}]
for row in cursor.fetchall():
print(row["name"])
Result Rows गिनना
cursor.rowcount, SELECT से results fetch करने के बाद पढ़ा जाए तो, बताता है कि query ने कितनी rows return कीं -- पूरे fetched data को देखे बिना जल्दी check करने के लिए उपयोगी कि कोई matching rows हैं भी या नहीं।
उदाहरण: Counting Result Rows
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers")
cursor.fetchall.return_value = [(1,), (2,), (3,)]
rows = cursor.fetchall()
cursor.rowcount = len(rows)
print(cursor.rowcount) # how many rows the query returned
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