if == " main ": init_db() while True: print("\n1. Add task\n2. List tasks\n3. Complete task\n4. Delete task\n5. Exit") choice = input("Choose: ") if choice == "1": add_task(input("Title: ")) elif choice == "2": list_tasks() elif choice == "3": complete_task(int(input("Task ID: "))) elif choice == "4": delete_task(int(input("Task ID: "))) elif choice == "5": break
def list_tasks(): with sqlite3.connect(DB_NAME) as conn: conn.row_factory = sqlite3.Row cursor = conn.cursor() cursor.execute("SELECT * FROM tasks") for row in cursor.fetchall(): status = "✓" if row["completed"] else "✗" print(f"{row['id']}. {row['title']} [{status}]") sqlite3 tutorial query python fixed
cursor.execute("INSERT OR IGNORE INTO users (name, email) VALUES (?, ?)", ("Bob", "bob@example.com")) Cause: No rows match, or you forgot to fetch. Fix: Use fetchone() for single row, fetchall() for all. Check your WHERE clause. Error 5: Data changes but doesn’t persist Cause: Forgot conn.commit() . Fix: Call commit() or use with sqlite3.connect() as shown above. 7. Using row_factory for Better Results By default, rows come back as tuples. That’s error-prone. Fix it by using row_factory to get dictionaries: if == " main ": init_db() while True: print("\n1
import sqlite3 print(sqlite3.sqlite_version) # Should output e.g., 3.40.1 If that runs, you’re ready. import sqlite3 Connect to (or create) a database file conn = sqlite3.connect("my_database.db") cursor = conn.cursor() Complete task\n4