Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sqlite_todo_list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Persistent To-Do List (with SQLite)

This is a simple but powerful command-line to-do list application that **saves your tasks permanently** using Python's built-in `sqlite3` database.

## Description

Unlike simple in-memory scripts, this to-do list creates a small database file (`todo.db`) in the same directory. Any tasks you add are saved to this database. You can close the script, turn off your computer, and when you run it again, your tasks will still be there.

## Features

* **Persistent Storage:** Uses `sqlite3` to save tasks.
* **Add Tasks:** Add new items to your list.
* **Remove Tasks:** Remove items by their unique ID.
* **List Tasks:** View all your current tasks, numbered by ID.
* **Database Creation:** Automatically creates the `todo.db` file and `tasks` table on first run.

## How to Run

1. Ensure you have Python 3 installed.
2. Run the script from your terminal:
```sh
python todo_list_db.py
```
3. A `todo.db` file will be created in the directory to store your tasks.
4. Follow the on-screen menu (1-4) to manage your list.

## Modules Used

* **`sqlite3`**: (Python's built-in module for an on-disk SQL database)
* **`sys`**: (Built-in module for exiting the script on a critical error)
93 changes: 93 additions & 0 deletions sqlite_todo_list/todo_list_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import sqlite3
import sys

# The name of the database file
DB_NAME = "todo.db"

def initialize_db(conn):
"""Creates the 'tasks' table if it doesn't already exist."""
try:
cursor = conn.cursor()
# Use 'IF NOT EXISTS' to safely run this every time
cursor.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL
);
""")
conn.commit()
print("Database initialized.")
except Exception as e:
print(f"Error initializing database: {e}")
sys.exit(1)

def add_task(conn):
"""Adds a new task to the database."""
desc = input("Enter the task description: ")
if not desc:
print("Task description cannot be empty.")
return

cursor = conn.cursor()
cursor.execute("INSERT INTO tasks (description) VALUES (?)", (desc,))
conn.commit()
print(f"Added task: '{desc}'")

def remove_task(conn):
"""Removes a task from the database by its ID."""
list_tasks(conn, quiet=True) # Show the list first
try:
task_id = int(input("Enter the task ID to remove: "))
cursor = conn.cursor()
# Use 'changes()' to see if a row was actually deleted
cursor.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
if cursor.rowcount == 0:
print(f"No task found with ID {task_id}.")
else:
conn.commit()
print(f"Removed task {task_id}.")
except ValueError:
print("Invalid ID. Please enter a number.")

def list_tasks(conn, quiet=False):
"""Lists all current tasks in the database."""
cursor = conn.cursor()
cursor.execute("SELECT id, description FROM tasks ORDER BY id")
rows = cursor.fetchall()

if not rows:
if not quiet:
print("\nYour to-do list is empty.")
else:
print("\n--- Your To-Do List ---")
for row in rows:
print(f" [{row[0]}] {row[1]}") # e.g., [1] Buy milk

def main():
"""Main application loop."""
# 'with' statement handles connection closing
with sqlite3.connect(DB_NAME) as conn:
initialize_db(conn)

while True:
print("\n--- Persistent To-Do List ---")
print("1. List tasks")
print("2. Add task")
print("3. Remove task")
print("4. Quit")
choice = input("Enter your choice (1-4): ")

if choice == '1':
list_tasks(conn)
elif choice == '2':
add_task(conn)
elif choice == '3':
remove_task(conn)
elif choice == '4':
print("Goodbye!")
break
else:
print("Invalid choice. Please enter a number from 1 to 4.")

if __name__ == "__main__":
main()