Skip to content

CRUD Project Starter

This starter outlines a minimal file structure and sample code to begin your command-line CRUD project.

Repository Structure

crud_starter/
├── README.md
├── main.py
└── database.py
- main.py – entry point for the CLI. - database.py – initializes the database and provides basic CRUD helpers. - README.md – setup instructions and example commands.

main.py

import logging
from database import init_db

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
)
logger = logging.getLogger(__name__)

def main() -> None:
    """Launch the CLI application and initialize required resources.

    This function sets up logging, initializes the SQLite database,
    and logs each step so you can trace the startup process.

    Args:
        None

    Returns:
        None

    Raises:
        sqlite3.Error: If the database cannot be created or accessed.

    Examples:
        >>> main()
        # Startup messages are logged to the console
    """
    logger.info("Starting CLI application")
    init_db()
    logger.info("Database initialized")
    print("Welcome to your CLI project. Add menu logic here.")


if __name__ == "__main__":
    main()

database.py

import logging
import sqlite3

logger = logging.getLogger(__name__)

def init_db() -> None:
    """Create the database and default table if needed.

    This function opens a SQLite connection, ensures the records table
    exists, commits the changes, and then closes the connection.

    Args:
        None

    Returns:
        None

    Raises:
        sqlite3.Error: If the table cannot be created or written to.

    Examples:
        >>> init_db()
        # Logs indicate whether the table was created
    """
    logger.info("Initializing database")
    conn = sqlite3.connect("records.db")
    conn.execute(
        "CREATE TABLE IF NOT EXISTS records (id INTEGER PRIMARY KEY, name TEXT)"
    )
    conn.commit()
    conn.close()
    logger.info("Database setup complete")

Copy this structure into your repository to jump start development. Expand the schema and command logic as you add features.