Online Computer Courses Classes and Training Program

Lesson 8 — Part 2: Student को SQLite Database में Save करना

 

बिल्कुल सही! ✅ Lesson 8 का पहला भाग सफल हो गया है।

आपके screenshot में school.db साफ दिखाई दे रहा है:

School_Fee_Management_Learning
│
├── venv
├── main.py
└── school.db   ✅

इसका मतलब Python ने SQLite database file successfully बना दी है। अब हम असल में Student को database में save करना सीखेंगे।

🎓 Lesson 8 — Part 2: Student को SQLite Database में Save करना

अभी हमारा flow:

Form
 ↓
Save Student
 ↓
Popup

अब इसे बदलेंगे:

Form
 ↓
Save Student
 ↓
Python
 ↓
SQLite
 ↓
school.db
 ↓
students table

1. save_student() Function बदलें

अभी आपके code में save_student() के अंदर popup वाला code है।

उसे पूरा हटाकर यह लिखें:

def save_student():
    name = student_name.text()
    father = father_name.text()
    mobile_number = mobile.text()
    student_class_value = student_class.text()
    fee = monthly_fee.text()

    connection = get_connection()
    cursor = connection.cursor()

    cursor.execute("""
        INSERT INTO students (
            student_name,
            father_name,
            mobile,
            class_name,
            monthly_fee
        )
        VALUES (?, ?, ?, ?, ?)
    """, (
        name,
        father,
        mobile_number,
        student_class_value,
        fee
    ))

    connection.commit()
    connection.close()

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

🧠 अब सबसे महत्वपूर्ण हिस्सा समझिए

यह line:

cursor.execute("""

SQLite को command भेजती है।

हमने लिखा:

INSERT INTO students

इसका मतलब:

students table में नया student डालो।


2. किन columns में data जाएगा?

हमने लिखा:

student_name,
father_name,
mobile,
class_name,
monthly_fee

और फिर:

VALUES (?, ?, ?, ?, ?)

इन ? की जगह Python हमारे actual values डाल देगा।

उदाहरण:

Rahul Kumar
Ramesh Kumar
9876543210
V
1000

Database में record बनेगा:

idstudent_namefather_namemobileclass_namemonthly_fee
1Rahul KumarRamesh Kumar9876543210V1000

3. connection.commit() क्यों?

यह line:

connection.commit()

बहुत महत्वपूर्ण है।

इसका मतलब:

Database में किए गए बदलाव को save/confirm करो।

अगर commit() नहीं करेंगे, तो INSERT के बाद data स्थायी रूप से save नहीं हो सकता।

इसे ऐसे याद रखें:

INSERT
   ↓
commit()
   ↓
SAVE

4. connection.close() क्यों?

connection.close()

इसका मतलब:

Database connection अब बंद कर दो।

हमारा काम पूरा हो गया, इसलिए connection बंद करना अच्छी practice है।


5. अब Student Save करके Test करें

Program बंद करके फिर चलाएँ:

python main.py

Form में भरें:

Student Name:    Rahul Kumar
Father Name:     Ramesh Kumar
Mobile Number:   9876543210
Class:           V
Monthly Fee:     1000

फिर:

Save Student

दबाएँ।

अब popup आना चाहिए:

Student saved successfully!

🎉 लेकिन असली test अभी बाकी है।




6. Database में सचमुच Save हुआ या नहीं?

Program बंद करें।

फिर हम database को Python से पढ़ेंगे।

main.py में अभी एक temporary function बनाते हैं:

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

    cursor.execute("SELECT * FROM students")

    students = cursor.fetchall()

    connection.close()

    print(students)

फिर temporary रूप से:

show_students()

को:

create_table()

के नीचे रखें।

यानी:

create_table()
show_students()

app = QApplication(sys.argv)

अब program चलाएँ:

python main.py

Terminal में आपको कुछ ऐसा दिखाई देना चाहिए:

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



इसका मतलब है:

🎉 Rahul Kumar वास्तव में SQLite database में save हो गया है।


⚠️ एक जरूरी बात

show_students() अभी सिर्फ सीखने और test करने के लिए है।

अभी इसे final software में नहीं रखेंगे।

अगले चरण में हम इससे बेहतर तरीका बनाएँगे:

Student Form
      ↓
Save
      ↓
SQLite
      ↓
Student List
      ↓
┌─────────────────────────────────────┐
│ ID │ Name        │ Class │ Fee      │
├─────────────────────────────────────┤
│ 1  │ Rahul Kumar │ V     │ ₹1000    │
└─────────────────────────────────────┘

यानी हम पहली बार Database से data वापस निकालकर application में दिखाएँगे।

अभी आपका Task

  1. save_student() को ऊपर वाला code दें।

  2. Rahul Kumar को फिर से save करें।

  3. show_students() से test करें।

  4. Terminal में database record दिखाई दे तो उसका screenshot भेजें।

फिर हम Lesson 9 — SQLite से Student Data पढ़ना और Student List बनाना शुरू करेंगे।

Post a Comment

0 Comments