Here's a 30-minute SQLite tutorial divided into four modules to explain CRUD principles for undergraduate students. Each module takes about 7-8 minutes and includes theory, examples, and exercises.
๐ฏ Goal of the Tutorial
- Learn the basics of SQLite.
- Understand CRUD (Create, Read, Update, Delete) operations.
- Use SQLite commands to manage a database.
๐ Module 1: Introduction to SQLite & Setting Up (7 mins)
๐น What is SQLite?
- A lightweight, self-contained database.
- No need for a separate server (unlike MySQL/PostgreSQL).
- Used in mobile apps, embedded systems, and local applications.
๐น Installing SQLite
Windows:
- Download SQLite from SQLite official site.
- Extract and open the
sqlite3.exe
file. - Run:
sqlite3
Linux/macOS:
- Install via terminal:
sudo apt install sqlite3 # Ubuntu/Debian brew install sqlite # macOS
- Run SQLite:
sqlite3
๐ Exercise 1: Creating a Database
sqlite3 students.db
โ๏ธ This creates a students.db file where we will store our tables.
๐ Module 2: Creating and Reading Data (7 mins)
๐น Creating a Table (C in CRUD)
We create a students table:
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE,
age INTEGER,
course TEXT
);
๐น Inserting Data
INSERT INTO students (name, email, age, course)
VALUES ('Alice', 'alice@example.com', 20, 'Computer Science');
๐น Reading Data (R in CRUD)
SELECT * FROM students;
๐ Exercise 2: Insert and Read
- Insert two more student records.
- Run
SELECT * FROM students;
to check.
โ๏ธ Module 3: Updating and Deleting Data (7 mins)
๐น Updating Data (U in CRUD)
Updating Aliceโs course:
UPDATE students
SET course = 'Data Science'
WHERE name = 'Alice';
๐น Deleting Data (D in CRUD)
DELETE FROM students WHERE name = 'Alice';
๐ Exercise 3:
- Update another studentโs age.
- Delete a student record.
๐ Module 4: Advanced Queries & Best Practices (7 mins)
๐น Filtering Data
SELECT * FROM students WHERE age > 18;
๐น Sorting Data
SELECT * FROM students ORDER BY name ASC;
๐น Counting Records
SELECT COUNT(*) FROM students;
๐ Final Exercise
- Retrieve students older than 21.
- Count the number of students.
Dropping Tables and Databases in SQLite
SQLite allows you to delete tables using DROP TABLE
, but it does not support dropping databases directly since an SQLite database is just a file.
๐น Dropping a Table in SQLite
To delete a table along with all its data, use:
DROP TABLE table_name;
Example
DROP TABLE students;
โ๏ธ This removes the students
table permanently.
โ Warning: This action cannot be undone.
๐น Dropping a Database in SQLite
Since SQLite databases are just files, you delete a database by removing the database file from the system.
Steps to Drop an SQLite Database
- Find the database file (e.g.,
mydatabase.db
). - Delete it manually using the terminal or command prompt.
Using Command Line
Windows
del mydatabase.db
Linux/macOS
rm mydatabase.db
โ๏ธ This permanently deletes the database file.
โ Warning: This cannot be undone unless you have a backup.
๐น Checking Before Dropping
Before dropping a table, check if it exists:
DROP TABLE IF EXISTS students;
โ๏ธ This prevents errors if the table doesnโt exist.
๐น Alternative: Clearing a Table Instead of Dropping
If you just want to clear all data but keep the table structure, use:
DELETE FROM students;
โ๏ธ Removes all records but keeps the table.
OR
DELETE FROM students WHERE 1=1;
โ๏ธ Same as above but can be used dynamically in scripts.
Would you like a script to automate table deletions? ๐
๐ฏ Summary of the Tutorial
โ๏ธ Module 1: Setup and creating a database
โ๏ธ Module 2: Creating and reading records
โ๏ธ Module 3: Updating and deleting data
โ๏ธ Module 4: Advanced queries
๐ Next Steps
- Try integrating SQLite with Python, Node.js, or JavaScript.
- Experiment with joins, indexing, and transactions.
Would you like me to prepare slides or code files for this tutorial? ๐
No comments:
Post a Comment