This session bridges the gap between shell scripting and structured data management by introducing the basics of working with relational databases directly from the command line using SQLite.
Learning Objectives
- Understand the core principles of Relational Database Management Systems (RDBMS).
- Learn the essential SQL commands for data definition and manipulation (DDL/DML).
- Use the
sqlite3command-line utility to interact with a database file. - Integrate database operations into Bash scripts.
Topics Covered
1. Relational Database Concepts
- Tables, Rows, and Columns: Defining the basic structure.
- Primary Keys and Data Types: Brief overview of common types (INTEGER, TEXT, REAL).
2. Essential SQL Commands (Using sqlite3)
- Data Definition Language (DDL):
CREATE TABLE: Defining a new table structure.- Data Manipulation Language (DML):
INSERT INTO: Adding new records.SELECT: Querying data (includingWHEREclauses for filtering).UPDATE: Modifying existing records.DELETE FROM: Removing records.
3. Command-Line Database Interaction
- Invoking
sqlite3: Opening a database file (sqlite3 inventory.db). - Executing SQL:
- Inline:
sqlite3 db.sqlite "SELECT * FROM users;" -
Heredoc (Recommended for Scripts):
bash sqlite3 inventory.db <<EOF INSERT INTO Products (Name, Quantity) VALUES ('Apples', 50); SELECT * FROM Products WHERE Quantity < 100; EOF -
Output Formatting: Use
-csv,-column, or-jsonflags to get data in a format scripts can easily parse. - SQLite Dot-Commands (Advanced):
.mode csv: Switch output to CSV..import file.csv table: Import data from a CSV file..schema: Show the database structure.
4. Transactions and Integrity (Advanced)
- Transactions: Grouping multiple commands for safety.
sql
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Lab/Assessment Focus
Goal: Build a scriptable inventory manager.
- Initialize: Create a script that initializes
inventory.dbusing a Heredoc. - Import: Export sample data from a CSV file into the
Productstable using.import. - Query: Write a function
get_low_stock()that queries the database for products with quantity less than a threshold passed as an argument. - Format: Use the
-jsonoutput format and pipe it to a script that simulates sending an alert (just echo for now). - Transaction: Implement an
update_stockfunction that uses a transaction to ensure data integrity.
Advanced Topic References
- SQLite Command Line Shell Tutorial: Official guide to the
sqlite3utility. - SQL Tutorial for Beginners: Comprehensive SQL language reference.
- Bash Scripting and SQLite Integration: Examples of piping data into the CLI tool.