Skip to content

Latest commit

 

History

History

database

Folders and files

README.md

Python Database Operations & Management Module

Welcome to the Python Database Module. This directory provides a structured, educational, and production-ready collection of Python scripts demonstrating relational database interactions using both SQLite (Python standard library) and MySQL (external server connection driver).


πŸš€ Beginner's Guide: Core Database Concepts & Definitions

If you are new to databases, this section will help you understand all fundamental concepts before writing code.

πŸ“š Core Terminology Definitions

Term Definition Analogous Concept
Database (DB) An organized collection of structured data stored electronically on a computer system. A digital filing cabinet or workbook.
Relational Database (RDBMS) A database that organizes data into tables linked by predefined relationships. Excel workbook with multiple interlinked sheets.
Table A collection of related data entries organized in vertical columns and horizontal rows. A single spreadsheet tab/sheet.
Column / Field A vertical data attribute representing a specific type of information (e.g., name, age). A single spreadsheet column header.
Row / Record A single horizontal data entry containing values for all attributes of an item. A single spreadsheet row filled with data.
Primary Key (PK) A unique identifier for every record in a table (e.g., id=101). No two rows can share the same PK. A Social Security Number or Student ID.
Foreign Key (FK) A column in one table that references the Primary Key of another table to create a link between them. A reference code pointing to another sheet.
SQL Structured Query Language - The universal language used to communicate with relational databases. The command language for database requests.
CRUD Acronym for the 4 basic database operations: Create, Read, Update, and Delete. Add, View, Edit, Remove operations.

πŸ”€ Database Data Types (Definitions & Examples)

Databases enforce data types on each column to ensure data consistency and integrity.

Data Type SQL Keyword Description Python Equivalent Example Values
Integer INTEGER / INT Whole numbers (positive, negative, or zero). int 1, 42, -10, 2026
Floating Point REAL / FLOAT / DOUBLE Decimal numbers with fractional parts. float 19.99, 3.14159, -0.05
Text / String TEXT / VARCHAR(N) Text characters or strings. VARCHAR(N) limits string length to N. str "Alice", "Toyota Camry", "email@example.com"
Binary Data BLOB Binary Large Object. Used for raw bytes like images or encrypted data. bytes b'\x89PNG...'
Null Value NULL Represents missing, empty, or unknown values. None None

πŸ’» OS-Specific Installation Guide

Python includes the sqlite3 module by default. For external MySQL database management, follow the setup steps for your operating system below.

πŸͺŸ 1. Windows Installation Guide

Step 1: Open Command Prompt or PowerShell

Press Win + R, type cmd or powershell, and press Enter.

Step 2: Create and Activate a Virtual Environment

:: Navigate to your project folder
cd C:\path\to\your\project\database

:: Create virtual environment named 'venv'
python -m venv venv

:: Activate virtual environment
venv\Scripts\activate

Step 3: Install Required Dependencies

pip install --upgrade pip
pip install -r requirements.txt

🍎 2. macOS Installation Guide

Step 1: Open Terminal (Cmd + Space -> Terminal)

Step 2: Create and Activate Virtual Environment

cd /path/to/your/project/database
python3 -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

🐧 3. Linux Installation Guide (Ubuntu / Debian / Fedora / Arch)

Step 1: Open Terminal (Ctrl + Alt + T)

Step 2: Create and Activate Virtual Environment

cd /path/to/your/project/database
python3 -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

πŸ§ͺ Automated Testing & Security Verification

To guarantee code security, data integrity, and prevent regression bugs or SQL Injection vulnerabilities, an automated unit test suite is included in the tests/ directory.

πŸ›‘οΈ What is Tested for Security & Stability?

  1. SQL Injection Defense: Tests tautology attacks (' OR '1'='1), piggybacked DROP TABLE attempts, and UNION query injection payloads against all search functions to ensure parameters are properly bound.
  2. SQLite Operations Test: Tests connection management, table creation, batch static insertion, dynamic user input handling, filtering, limit pagination, updates, and deletions in isolated temporary database instances (tempfile).
  3. MySQL Manager Test: Tests configuration validation, connection failure handling, fallback states, and mock query executions.

πŸƒ How to Run the Test Suite

Run the full test suite using Python's built-in unittest runner:

python3 -m unittest discover -v -s tests

Or using pytest:

pytest -v tests/

πŸ“ Directory Architecture

database/
β”œβ”€β”€ README.md                  # Complete documentation, setup, and testing guide
β”œβ”€β”€ requirements.txt           # External package dependencies (MySQL drivers, pytest)
β”œβ”€β”€ mysql_db.py                # Object-oriented MySQL database manager
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py            # Test package marker
β”‚   β”œβ”€β”€ test_sqlite_modules.py # SQLite CRUD operations test suite
β”‚   β”œβ”€β”€ test_mysql_manager.py  # MySQLDatabaseManager test suite
β”‚   └── test_security_sql_injection.py # Security & SQL Injection test suite
└── sqlite_db/
    β”œβ”€β”€ __init__.py            # Package marker
    β”œβ”€β”€ beginner_starter.py    # Step-by-step beginner tutorial script
    β”œβ”€β”€ connect_db.py          # Connection and cursor initialization demo
    β”œβ”€β”€ create_table.py         # Table schema creation DDL demo
    β”œβ”€β”€ insert_static_data.py  # Static dataset batch insertion demo
    β”œβ”€β”€ insert_dynamic_data.py # Dynamic user input record insertion demo
    β”œβ”€β”€ read_all_data.py       # Full table query (SELECT *) demo
    β”œβ”€β”€ read_filtered_data.py  # Conditional row query (WHERE) demo
    β”œβ”€β”€ read_by_user_input.py  # Dynamic single-parameter search demo
    β”œβ”€β”€ read_by_multiple_inputs.py # Compound multi-parameter search demo
    β”œβ”€β”€ read_with_limit.py     # Pagination query (LIMIT) demo
    β”œβ”€β”€ update_data.py         # Record update (UPDATE) demo
    β”œβ”€β”€ delete_data.py         # Row deletion (DELETE) demo
    β”œβ”€β”€ delete_data_with_commit.py # Persistent deletion with explicit commit demo
    └── email_counter_crud.py  # Log aggregation CRUD counter demo

πŸ’‘ Best Practices Checklist

  1. Use Parameterized Queries: Always use ? (SQLite) or %s (MySQL) to pass variables safely and prevent SQL injection.
  2. Always Use Context Managers: Use with sqlite3.connect(...) as connection: to handle connection cleanup automatically.
  3. Commit Transactions: DML commands (INSERT, UPDATE, DELETE) require connection.commit() to persist changes to disk.
  4. Run Unit Tests: Execute python3 -m unittest discover -v -s tests before committing code changes.