बिल्कुल सही! ✅ 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 table1. 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इसका मतलब:
studentstable में नया student डालो।
2. किन columns में data जाएगा?
हमने लिखा:
student_name,
father_name,
mobile,
class_name,
monthly_feeऔर फिर:
VALUES (?, ?, ?, ?, ?)इन ? की जगह Python हमारे actual values डाल देगा।
उदाहरण:
Rahul Kumar
Ramesh Kumar
9876543210
V
1000Database में record बनेगा:
| id | student_name | father_name | mobile | class_name | monthly_fee |
|---|---|---|---|---|---|
| 1 | Rahul Kumar | Ramesh Kumar | 9876543210 | V | 1000 |
3. connection.commit() क्यों?
यह line:
connection.commit()बहुत महत्वपूर्ण है।
इसका मतलब:
Database में किए गए बदलाव को save/confirm करो।
अगर commit() नहीं करेंगे, तो INSERT के बाद data स्थायी रूप से save नहीं हो सकता।
इसे ऐसे याद रखें:
INSERT
↓
commit()
↓
SAVE4. connection.close() क्यों?
connection.close()इसका मतलब:
Database connection अब बंद कर दो।
हमारा काम पूरा हो गया, इसलिए connection बंद करना अच्छी practice है।
5. अब Student Save करके Test करें
Program बंद करके फिर चलाएँ:
python main.pyForm में भरें:
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.pyTerminal में आपको कुछ ऐसा दिखाई देना चाहिए:
[(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
save_student()को ऊपर वाला code दें।Rahul Kumar को फिर से save करें।
show_students()से test करें।Terminal में database record दिखाई दे तो उसका screenshot भेजें।
फिर हम Lesson 9 — SQLite से Student Data पढ़ना और Student List बनाना शुरू करेंगे।
0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें