fork download
  1. import mysql.connector
  2.  
  3. # Connect to the MySQL database
  4. def connect_to_db():
  5. return mysql.connector.connect(
  6. host="localhost", # Change this to your database host
  7. user="root", # Change to your MySQL username
  8. password="password", # Change to your MySQL password
  9. database="todo_db" # The name of your database
  10. )
  11.  
  12. # Create the database and table
  13. def setup_database():
  14. conn = connect_to_db()
  15. cursor = conn.cursor()
  16. cursor.execute("CREATE DATABASE IF NOT EXISTS todo_db")
  17. cursor.execute("USE todo_db")
  18. cursor.execute("""
  19. CREATE TABLE IF NOT EXISTS tasks (
  20. id INT AUTO_INCREMENT PRIMARY KEY,
  21. task VARCHAR(255),
  22. status VARCHAR(50)
  23. )
  24. """)
  25. conn.commit()
  26. conn.close()
  27.  
  28. # Add a task
  29. def add_task(task, status="Pending"):
  30. conn = connect_to_db()
  31. cursor = conn.cursor()
  32. cursor.execute("INSERT INTO tasks (task, status) VALUES (%s, %s)", (task, status))
  33. conn.commit()
  34. conn.close()
  35. print("Task added successfully!")
  36.  
  37. # View all tasks
  38. def view_tasks():
  39. conn = connect_to_db()
  40. cursor = conn.cursor()
  41. cursor.execute("SELECT * FROM tasks")
  42. tasks = cursor.fetchall()
  43. conn.close()
  44. print("\nTo-Do List:")
  45. for task in tasks:
  46. print(f"ID: {task[0]}, Task: {task[1]}, Status: {task[2]}")
  47.  
  48. # Update a task's status
  49. def update_task_status(task_id, status):
  50. conn = connect_to_db()
  51. cursor = conn.cursor()
  52. cursor.execute("UPDATE tasks SET status = %s WHERE id = %s", (status, task_id))
  53. conn.commit()
  54. conn.close()
  55. print("Task updated successfully!")
  56.  
  57. # Delete a task
  58. def delete_task(task_id):
  59. conn = connect_to_db()
  60. cursor = conn.cursor()
  61. cursor.execute("DELETE FROM tasks WHERE id = %s", (task_id,))
  62. conn.commit()
  63. conn.close()
  64. print("Task deleted successfully!")
  65.  
  66. # Main menu
  67. def main_menu():
  68. setup_database()
  69. while True:
  70. print("\nTo-Do List Menu:")
  71. print("1. Add Task")
  72. print("2. View Tasks")
  73. print("3. Update Task Status")
  74. print("4. Delete Task")
  75. print("5. Exit")
  76.  
  77. choice = input("Enter your choice: ")
  78.  
  79. if choice == "1":
  80. task = input("Enter the task: ")
  81. add_task(task)
  82. elif choice == "2":
  83. view_tasks()
  84. elif choice == "3":
  85. task_id = int(input("Enter task ID to update: "))
  86. status = input("Enter new status (Pending/Completed): ")
  87. update_task_status(task_id, status)
  88. elif choice == "4":
  89. task_id = int(input("Enter task ID to delete: "))
  90. delete_task(task_id)
  91. elif choice == "5":
  92. print("Exiting To-Do List. Goodbye!")
  93. break
  94. else:
  95. print("Invalid choice. Please try again.")
  96.  
  97. # Run the application
  98. if __name__ == "__main__":
  99. main_menu()
Success #stdin #stdout 0.03s 25760KB
stdin
Standard input is empty
stdout
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()