Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apursley2012/corner-grocer/llms.txt
Use this file to discover all available pages before exploring further.
db_handler.py provides the full SQLite data access layer for Corner Grocer. Each function corresponds to a single, discrete database operation — connecting, schema creation, upsert, retrieval, and deletion. You can import individual functions directly into application code or use them through the higher-level CornerGrocerApp wrapper class in corner_grocer_main.py.
Import
Functions
connect_to_db
db_name using sqlite3.connect(). On success, prints a confirmation message and returns the live connection object. On failure, prints the sqlite3.Error and returns None.
The file path for the SQLite database. Use
':memory:' for an in-memory database, or a relative/absolute file path such as 'grocery.db'. If the file does not exist, SQLite creates it automatically.sqlite3.Connection on success, None on error.
create_table
CREATE TABLE IF NOT EXISTS statement that defines the grocery_frequency table. If the table already exists, the call is a no-op. Calls sys.exit(1) if the statement fails, ensuring the application does not continue without a valid schema.
The SQL executed is:
An open SQLite connection object, typically obtained from
connect_to_db.None. Commits the DDL statement and prints a verification message.
Error handling: Catches sqlite3.Error, prints the error, and calls sys.exit(1).
insert_or_update_item
grocery_frequency for a row whose item_name matches item. If a row is found, it increments frequency by 1 and issues an UPDATE. If no row is found, it inserts a new row with frequency = 1. Both paths commit the transaction.
An open SQLite connection object.
The item name to insert or update. Should be pre-normalized to lowercase before calling this function to maintain consistent key casing.
None. Modifies the grocery_frequency table in place.
Error handling: Catches sqlite3.Error and prints an error message. Does not raise or exit — the failed operation is silently skipped.
get_all_frequencies
grocery_frequency ordered alphabetically by item_name (A–Z) and returns them as a plain Python dictionary.
An open SQLite connection object.
dict — keys are item_name strings, values are frequency integers. The dictionary preserves insertion order from the ORDER BY item_name ASC query result. Returns an empty {} on error.
Error handling: Catches sqlite3.Error, prints an error message, and returns {}.
get_frequency_for_item
grocery_frequency for a single row where item_name equals item and returns its frequency value. Returns 0 if no matching row is found, matching the same sentinel value used by GroceryFrequency.lookup_item_frequency.
An open SQLite connection object.
The item name to look up. Should be lowercase to match stored keys.
int — the frequency count for the item, or 0 if the item does not exist in the table.
Error handling: Catches sqlite3.Error, prints an error message, and returns 0.
get_frequencies_sorted_by_count
grocery_frequency sorted by frequency descending, then by item_name ascending for ties, and returns them as a plain Python dictionary. This ordering matches the “Sort items by frequency (high → low)” menu option.
An open SQLite connection object.
dict — keys are item_name strings, values are frequency integers, ordered by descending frequency count (ties broken alphabetically). Returns an empty {} on error.
Error handling: Catches sqlite3.Error, prints an error message, and returns {}.
clear_all_data
DELETE FROM grocery_frequency to remove every row from the table, then commits the transaction. The table structure is preserved — only the data rows are removed. Prints a confirmation message on success.
An open SQLite connection object.
None.
Error handling: Catches sqlite3.Error and prints an error message.