🐍 Python MySQL Fetch Records Explained | SELECT Query | fetchall | Python Basics

Pubblicato il: 22 dicembre 2019
sul canale di: Guhan Tech Zone
106
3

In this video, you will learn how to retrieve records from MySQL table using Python step by step.

We’ll understand:

✔ SELECT query
✔ fetchone vs fetchall
✔ Using cursor
✔ Looping records
✔ enumerate explained
✔ Practical example

This Python MySQL tutorial is specially designed for beginners.

🧠 What is Fetch / Retrieve Records?

Fetching records means:

👉 Reading data from database table.

Here we retrieve all records from user table.

✅ Complete Example (From Your Code)
import mysql.connector

conn = mysql.connector.connect(
host="localhost",
username="root",
password="",
db="pythondb"
)

cursor = conn.cursor()
cursor.execute("select * from user")

res = cursor.fetchone()

res = cursor.fetchall()

for i in res:
for x, y in enumerate(i):
print(x, y)

🧠 Step by Step Explanation
1️⃣ Execute SELECT Query
cursor.execute("select * from user")

select * means fetch all columns.

2️⃣ fetchall()
res = cursor.fetchall()

fetchall returns:

👉 List of tuples.

Each tuple = one row.

🔁 fetchone() (Commented in Your Code)

res = cursor.fetchone()

fetchone returns:

👉 Only ONE record.

You commented it to explain another method.

So:

fetchone → single row
fetchall → all rows

3️⃣ Loop Through Records
for i in res:

i holds one row.

4️⃣ enumerate Explained
for x, y in enumerate(i):
print(x, y)

enumerate gives:

x → column index
y → column value

Example:
0 1
1 Anbu

⭐ Example Output
0 2
1 Anbu

🧠 Simple Understanding

res = [(2,"Anbu"), (3,"Raja")]

Outer loop → rows
Inner loop → columns

⭐ Key Takeaways

✔ SELECT retrieves data
✔ fetchone gets single row
✔ fetchall gets all rows
✔ enumerate gives index + value
✔ Cursor executes queries

⚠ Common Beginner Mistakes

❌ Forgetting execute()
❌ Using fetchall without select
❌ MySQL not running
❌ Wrong table name

📚 What You’ll Learn

Python MySQL fetch
SELECT query
fetchall
fetchone
enumerate

🔜 Upcoming Videos

Update Records
Delete Records
Close Connection

👍 Like
💬 Comment doubts
🔔 Subscribe for Python tutorials

python mysql fetch records
python mysql select
fetchall python mysql
python database retrieve
mysql python tutorial
learn python step by step

#Python
#MySQL
#PythonMySQL
#FetchRecords
#Database
#XAMPP
#LearnPython
#PythonBasics
#PythonTutorial
#Programming


In questa pagina del sito puoi guardare il video online 🐍 Python MySQL Fetch Records Explained | SELECT Query | fetchall | Python Basics della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Guhan Tech Zone 22 dicembre 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 106 volte e gli è piaciuto 3 spettatori. Buona visione!