🟡 Advanced Python · Lesson 27
Python के साथ SQLite Database
Python SQLite Database क्या है?
Python SQLite Database ka matlab hai: SQLite is a lightweight database included with Python. It is useful for small applications, local storage and practice projects. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki saving records locally jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye saving records locally me kaam aata hai.
- Ye small desktop applications se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| sqlite3 | Built-in module used to work with SQLite databases. |
| table | table is an important term in this topic. |
| insert | insert is an important term in this topic. |
| select | select is an important term in this topic. |
| commit | commit is an important term in this topic. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
Basic Pattern
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.execute("CREATE TABLE students (name TEXT, marks INTEGER)")
cur.execute("INSERT INTO students VALUES (?, ?)", ("Riya", 94))
conn.commit()
cur.execute("SELECT * FROM students")
print(cur.fetchall())Complete Example Program
Python – database-sqlite.py
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.execute("CREATE TABLE students (name TEXT, marks INTEGER)")
cur.execute("INSERT INTO students VALUES (?, ?)", ("Riya", 94))
conn.commit()
cur.execute("SELECT * FROM students")
print(cur.fetchall())
conn.close()Expected Output
[('Riya', 94)]
Program Explanation
import sqlite3imports ready-made features from a module/library.conn = sqlite3.connect(":memory:")stores a value in conn.cur = conn.cursor()stores a value in cur.cur.execute("CREATE TABLE students (name TEXT, marks INTEGER)")performs the next step of the program logic.cur.execute("INSERT INTO students VALUES (?, ?)", ("Riya", 94))performs the next step of the program logic.conn.commit()performs the next step of the program logic.cur.execute("SELECT * FROM students")performs the next step of the program logic.
Practical Uses
- Saving records locally.
- Small desktop applications.
- Database practice projects.
Common Mistakes
- Making code complex when a simple function or class is enough.
- Not handling possible errors or edge cases.
- Mixing project dependencies instead of using a virtual environment.
Practice Tasks
- Program ko
database-sqlite.pyfile me type karke run karein. - Values change karke output compare karein.
- saving records locally par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
SQLite Database with Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.