Monday, March 10, 2025

#8.1 SQLite tutorial basics

 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:

  1. Download SQLite from SQLite official site.
  2. Extract and open the sqlite3.exe file.
  3. Run:
    sqlite3
    

Linux/macOS:

  1. Install via terminal:
    sudo apt install sqlite3   # Ubuntu/Debian
    brew install sqlite        # macOS
    
  2. 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

  1. Insert two more student records.
  2. 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:

  1. Update another studentโ€™s age.
  2. 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

  1. Retrieve students older than 21.
  2. 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

  1. Find the database file (e.g., mydatabase.db).
  2. 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

#11 @Output

  Understanding @Output() in Angular 19 The @Output() decorator in Angular 19 is used to send data from a child component to a parent co...