🟡 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

TermMeaning
sqlite3Built-in module used to work with SQLite databases.
tabletable is an important term in this topic.
insertinsert is an important term in this topic.
selectselect is an important term in this topic.
commitcommit 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 sqlite3 imports 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

  1. Program ko database-sqlite.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. saving records locally par ek छोटा example banayen.
  4. 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.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.