Online Computer Courses Classes and Training Program

Lesson 9 — Database से Student Data पढ़ना

 


बहुत बढ़िया! 🎉 Lesson 8 पूरी तरह सफल है।

आपके screenshot में सबसे महत्वपूर्ण result है:

[(1, 'Rahul Kumar', 'Ramesh Kumar', '9876543210', 'V', 1000.0)]

इसका मतलब:

  • id → 1

  • Student Name → Rahul Kumar

  • Father Name → Ramesh Kumar

  • Mobile → 9876543210

  • Class → V

  • Monthly Fee → ₹1000

और सबसे महत्वपूर्ण बात:

Rahul Kumar का record अब school.db SQLite database में वास्तव में save हो चुका है।

अब हम अगले चरण पर जाएँगे।


🎓 Lesson 9 — Database से Student Data पढ़ना

अभी हमने database में data डालना (INSERT) सीख लिया।

अब सीखेंगे:

Database से data वापस निकालना (SELECT)

अभी हमारा software data को सिर्फ Terminal में दिखा रहा है:

[(1, 'Rahul Kumar', 'Ramesh Kumar', '9876543210', 'V', 1000.0)]

लेकिन असली software में हमें ऐसा Student List चाहिए:

┌────┬──────────────┬──────────────┬────────────┬───────┬────────────┐
│ ID │ Student Name │ Father Name  │ Mobile     │ Class │ Fee        │
├────┼──────────────┼──────────────┼────────────┼───────┼────────────┤
│ 1  │ Rahul Kumar  │ Ramesh Kumar │ 9876543210 │ V     │ ₹1,000     │
└────┴──────────────┴──────────────┴────────────┴───────┴────────────┘

इसके लिए हम PySide6 का:

QTableWidget

सीखेंगे।


1. पहले QTableWidget import करें

अपने import section में जोड़ें:

QTableWidget,
QTableWidgetItem

इसलिए आपका import section अब लगभग ऐसा होगा:

from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QLabel,
    QPushButton,
    QVBoxLayout,
    QFormLayout,
    QMessageBox,
    QLineEdit,
    QTableWidget,
    QTableWidgetItem
)

2. Student Table बनाना

हम main_layout के अंदर एक table बनाएँगे।

Student fields के बाद और Save Student button से पहले यह code डालें:

# Student Table
student_table = QTableWidget()

student_table.setColumnCount(6)

student_table.setHorizontalHeaderLabels([
    "ID",
    "Student Name",
    "Father Name",
    "Mobile",
    "Class",
    "Monthly Fee"
])

main_layout.addWidget(student_table)

🧠 इसे समझें

QTableWidget

student_table = QTableWidget()

एक खाली table बनाता है।


setColumnCount(6)

हमारे पास 6 columns हैं:

ID
Student Name
Father Name
Mobile
Class
Monthly Fee

इसलिए:

setColumnCount(6)

Headers

student_table.setHorizontalHeaderLabels(...)

इससे table के ऊपर headings दिखाई देंगी।


3. Database से Students पढ़ने का Function

अब show_students() को बदलते हैं।

पुराना:

def show_students():
    connection = get_connection()
    cursor = connection.cursor()

    cursor.execute("SELECT * FROM students")

    students = cursor.fetchall()

    connection.close()

    print(students)

इसे अभी हटा दें और यह नया function बनाएँ:

def load_students():
    connection = get_connection()
    cursor = connection.cursor()

    cursor.execute("""
        SELECT id, student_name, father_name, mobile, class_name, monthly_fee
        FROM students
        ORDER BY id DESC
    """)

    students = cursor.fetchall()

    connection.close()

    student_table.setRowCount(len(students))

    for row_number, student in enumerate(students):
        for column_number, value in enumerate(student):
            student_table.setItem(
                row_number,
                column_number,
                QTableWidgetItem(str(value))
            )

🧠 अब इसे धीरे-धीरे समझते हैं

Database से data लेना

cursor.execute("""
    SELECT id, student_name, father_name, mobile, class_name, monthly_fee
    FROM students
    ORDER BY id DESC
""")

SELECT का मतलब:

Database से data पढ़ो।


fetchall()

students = cursor.fetchall()

इसका मतलब:

जितने records मिले, उन्हें सभी प्राप्त करो।

हमारे database में अभी Rahul है, इसलिए:

students
↓
[
    (1, Rahul Kumar, Ramesh Kumar, ...)
]

कितनी rows चाहिए?

student_table.setRowCount(len(students))

अगर database में:

1 Student

तो table में:

1 Row

अगर:

10 Students

तो:

10 Rows

4. Database Data को Table में डालना

यह हिस्सा बहुत महत्वपूर्ण है:

for row_number, student in enumerate(students):

यह एक-एक student पर काम करेगा।

फिर:

for column_number, value in enumerate(student):

यह उस student की एक-एक field पर जाएगा।

उदाहरण:

student
│
├── ID
├── Name
├── Father
├── Mobile
├── Class
└── Fee

फिर:

student_table.setItem(...)

उस value को table में रख देता है।


5. एक जरूरी बदलाव

अब हमारे application में table बनने के लिए student_table को पहले बनाना जरूरी है, लेकिन database functions ऊपर defined हैं।

इसलिए load_students() को function के रूप में define कर सकते हैं, लेकिन उसे student_table बनने के बाद call करेंगे

अभी:

create_table()
show_students()

app = QApplication(sys.argv)

है।

इसे बदलकर:

create_table()

app = QApplication(sys.argv)

कर दें।

load_students() अभी यहाँ call नहीं करना है।


6. Window बनने के बाद load_students() चलाएँ

यह हिस्सा:

save_button = QPushButton("Save Student")
save_button.clicked.connect(save_student)

main_layout.addWidget(save_button)

के बाद लिखें:

load_students()

और फिर:

window.show()

sys.exit(app.exec())

तो sequence होगा:

Database तैयार
      ↓
Application शुरू
      ↓
Window तैयार
      ↓
Student Table तैयार
      ↓
load_students()
      ↓
Database से students
      ↓
Table में दिखाई देंगे

7. अब Save के बाद भी Table Refresh करनी है

अभी अगर नया student save करेंगे, database में तो save हो जाएगा लेकिन table तुरंत update नहीं होगी।

इसलिए save_student() के अंत में:

QMessageBox.information(
    window,
    "Success",
    "Student saved successfully!"
)

के बाद यह जोड़ें:

load_students()

लेकिन यहाँ एक छोटा Python issue आएगा: save_student() के ऊपर load_students() function define होना चाहिए ताकि function name पहले से मौजूद हो।

इसलिए इस lesson में function order रखें:

get_connection()
        ↓
create_table()
        ↓
load_students()
        ↓
save_student()

🎯 हमारा नया Flow

अब software इस तरह काम करेगा:

┌─────────────────────────┐
│ Student Admission Form  │
│                         │
│ Name                    │
│ Father Name             │
│ Mobile                  │
│ Class                   │
│ Monthly Fee             │
│                         │
│ [ Save Student ]        │
└────────────┬────────────┘
             │
             ▼
        SQLite Database
             │
             ▼
┌─────────────────────────────────────────┐
│ Student List                            │
├────┬────────────┬───────────┬─────┬─────┤
│ ID │ Name       │ Father    │Class│ Fee │
├────┼────────────┼───────────┼─────┼─────┤
│ 1  │ Rahul      │ Ramesh    │ V   │1000 │
└────┴────────────┴───────────┴─────┴─────┘

यहाँ से हमारा project एक वास्तविक database application बनना शुरू हो गया है। 🚀

अभी आपका काम

ऊपर के बदलाव करें और:

python main.py

चलाएँ।

अगर Rahul Kumar table में दिखाई देता है, तो screenshot भेजिए।

फिर Lesson 10 — Student List में नया Student Add होने के बाद Table Automatically Refresh करना + Form Clear करना करेंगे।

Post a Comment

0 Comments