Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

SQLite Basics (Enhanced)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

🐍 Lesson 24: SQLite Basics

1. SQLite ဆိုတာဘာလဲ?

မြန်မာ → SQLite ဆိုတာ lightweight database engine တစ်ခု ဖြစ်ပြီး Python ထဲမှာ built-in ပါပြီးသား။ အခြား server တပ်ဆင်စရာမလိုဘဲ, ဖိုင်တစ်ခုအနေနဲ့ data ကို သိမ်းဆည်းနိုင်တယ်။

English → SQLite is a lightweight, file-based database engine built into Python. It doesn't require a separate server and stores data in a single file.

2. Why Use SQLite?

  • Setup လွယ် (Python ထဲမှာပါပြီးသား)
  • File-based (DB ကို .db ဖိုင်အနေနဲ့ သိမ်း)
  • Small projects, testing, prototyping အတွက် အဆင်ပြေ

3. အကျဉ်းချုပ်

✅ SQLite = lightweight, file-based DB

✅ Python မှာ sqlite3 module ပါပြီးသား

✅ CRUD operations → Create, Read, Update, Delete

✅ Use parameterized queries (?) for safety

python
# ===== 1. Connecting to SQLite =====
import sqlite3

conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()

print("Database connected")

# ===== 2. Creating a Table =====
print(f"\n===== Create Table =====")

cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    age INTEGER
)
""")
print("Table 'users' created")

# ===== 3. Inserting Data =====
print(f"\n===== Insert Data =====")

cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Sai", 25))
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Aye", 30))
conn.commit()
print("Data inserted")

# ===== 4. Reading Data =====
print(f"\n===== Read Data =====")

cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()

for row in rows:
    print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}")

# ===== 5. Updating Data =====
print(f"\n===== Update Data =====")

cursor.execute("UPDATE users SET age = ? WHERE name = ?", (26, "Sai"))
conn.commit()
print("Data updated")

# ===== 6. Deleting Data =====
print(f"\n===== Delete Data =====")

cursor.execute("DELETE FROM users WHERE name = ?", ("Aye",))
conn.commit()
print("Data deleted")

# ===== 7. Closing Connection =====
conn.close()
print("\nConnection closed")
You should see
Database connected ===== Create Table ===== Table 'users' created ===== Insert Data ===== Data inserted ===== Read Data ===== ID: 1, Name: Sai, Age: 25 ID: 2, Name: Aye, Age: 30 ===== Update Data ===== Data updated ===== Delete Data ===== Data deleted Connection closed
SQLite Basics (Enhanced) | Thuta Learning