import mysql.connector # Connect to the MySQL database def connect_to_db(): host="localhost", # Change this to your database host user="root", # Change to your MySQL username password="password", # Change to your MySQL password database="todo_db" # The name of your database ) # Create the database and table def setup_database(): conn = connect_to_db() cursor = conn.cursor() cursor.execute("CREATE DATABASE IF NOT EXISTS todo_db") cursor.execute("USE todo_db") cursor.execute(""" CREATE TABLE IF NOT EXISTS tasks ( id INT AUTO_INCREMENT PRIMARY KEY, task VARCHAR(255), status VARCHAR(50) ) """) conn.commit() conn.close() # Add a task def add_task(task, status="Pending"): conn = connect_to_db() cursor = conn.cursor() cursor.execute("INSERT INTO tasks (task, status) VALUES (%s, %s)", (task, status)) conn.commit() conn.close() print("Task added successfully!") # View all tasks def view_tasks(): conn = connect_to_db() cursor = conn.cursor() cursor.execute("SELECT * FROM tasks") tasks = cursor.fetchall() conn.close() print("\nTo-Do List:") for task in tasks: print(f"ID: {task[0]}, Task: {task[1]}, Status: {task[2]}") # Update a task's status def update_task_status(task_id, status): conn = connect_to_db() cursor = conn.cursor() cursor.execute("UPDATE tasks SET status = %s WHERE id = %s", (status, task_id)) conn.commit() conn.close() print("Task updated successfully!") # Delete a task def delete_task(task_id): conn = connect_to_db() cursor = conn.cursor() cursor.execute("DELETE FROM tasks WHERE id = %s", (task_id,)) conn.commit() conn.close() print("Task deleted successfully!") # Main menu def main_menu(): setup_database() while True: print("\nTo-Do List Menu:") print("1. Add Task") print("2. View Tasks") print("3. Update Task Status") print("4. Delete Task") print("5. Exit") choice = input("Enter your choice: ") if choice == "1": task = input("Enter the task: ") add_task(task) elif choice == "2": view_tasks() elif choice == "3": task_id = int(input("Enter task ID to update: ")) status = input("Enter new status (Pending/Completed): ") update_task_status(task_id, status) elif choice == "4": task_id = int(input("Enter task ID to delete: ")) delete_task(task_id) elif choice == "5": print("Exiting To-Do List. Goodbye!") break else: print("Invalid choice. Please try again.") # Run the application if __name__ == "__main__": main_menu()
Standard input is empty
import mysql.connector # Connect to the MySQL database def connect_to_db(): return mysql.connector.connect( host="localhost", # Change this to your database host user="root", # Change to your MySQL username password="password", # Change to your MySQL password database="todo_db" # The name of your database ) # Create the database and table def setup_database(): conn = connect_to_db() cursor = conn.cursor() cursor.execute("CREATE DATABASE IF NOT EXISTS todo_db") cursor.execute("USE todo_db") cursor.execute(""" CREATE TABLE IF NOT EXISTS tasks ( id INT AUTO_INCREMENT PRIMARY KEY, task VARCHAR(255), status VARCHAR(50) ) """) conn.commit() conn.close() # Add a task def add_task(task, status="Pending"): conn = connect_to_db() cursor = conn.cursor() cursor.execute("INSERT INTO tasks (task, status) VALUES (%s, %s)", (task, status)) conn.commit() conn.close() print("Task added successfully!") # View all tasks def view_tasks(): conn = connect_to_db() cursor = conn.cursor() cursor.execute("SELECT * FROM tasks") tasks = cursor.fetchall() conn.close() print("\nTo-Do List:") for task in tasks: print(f"ID: {task[0]}, Task: {task[1]}, Status: {task[2]}") # Update a task's status def update_task_status(task_id, status): conn = connect_to_db() cursor = conn.cursor() cursor.execute("UPDATE tasks SET status = %s WHERE id = %s", (status, task_id)) conn.commit() conn.close() print("Task updated successfully!") # Delete a task def delete_task(task_id): conn = connect_to_db() cursor = conn.cursor() cursor.execute("DELETE FROM tasks WHERE id = %s", (task_id,)) conn.commit() conn.close() print("Task deleted successfully!") # Main menu def main_menu(): setup_database() while True: print("\nTo-Do List Menu:") print("1. Add Task") print("2. View Tasks") print("3. Update Task Status") print("4. Delete Task") print("5. Exit") choice = input("Enter your choice: ") if choice == "1": task = input("Enter the task: ") add_task(task) elif choice == "2": view_tasks() elif choice == "3": task_id = int(input("Enter task ID to update: ")) status = input("Enter new status (Pending/Completed): ") update_task_status(task_id, status) elif choice == "4": task_id = int(input("Enter task ID to delete: ")) delete_task(task_id) elif choice == "5": print("Exiting To-Do List. Goodbye!") break else: print("Invalid choice. Please try again.") # Run the application if __name__ == "__main__": main_menu()