diff --git a/README.md b/README.md index 733d004..1ca7ff7 100644 --- a/README.md +++ b/README.md @@ -22,37 +22,37 @@ The [tasks.py](src/tasks.py) sample can be used to: * `Create` a database and table (necessary for the subsequent CRUD operations). ```bash - $ python3 tasks.py create + $ python3 src/tasks.py create ``` * `Drop` the database (and table). ```bash - $ python3 tasks.py drop + $ python3 src/tasks.py drop ``` * `Insert` a new tasks record. ```bash - $ python3 tasks.py add 'New Task Description' + $ python3 src/tasks.py add 'New Task Description' ``` * `Update` a task record's completion field (by specifying the `id` and `completion` value). ```bash - $ python3 tasks.py update 3 1 + $ python3 src/tasks.py updateStatus 3 1 ``` * `Select` and print all tasks. ```bash - $ python3 tasks.py show + $ python3 src/tasks.py show ``` * `Delete` a task record (by `id`). ```bash - $ python3 tasks.py delete 3 + $ python3 src/tasks.py delete 3 ``` ## Helpful Resources diff --git a/src/tasks.py b/src/tasks.py index 28e53e9..0fb3b18 100644 --- a/src/tasks.py +++ b/src/tasks.py @@ -1,10 +1,10 @@ import sys import mariadb -def createSchema(cur): - cur.execute("CREATE DATABASE todo") - print("todo database created") - cur.execute("""CREATE TABLE todo.tasks ( +def create_schema(cur): + cur.execute("CREATE DATABASE demo") + print("demo database created") + cur.execute("""CREATE TABLE demo.tasks ( id INT(11) unsigned NOT NULL AUTO_INCREMENT, description VARCHAR(500) NOT NULL, completed BOOLEAN NOT NULL DEFAULT 0, @@ -12,26 +12,26 @@ def createSchema(cur): )""") print("tasks table created") -def dropSchema(cur): - cur.execute("DROP DATABASE todo") - print("todo database and tasks table dropped") +def drop_schema(cur): + cur.execute("DROP DATABASE demo") + print("demo database and tasks table dropped") -def addTask(cur, description): - cur.execute("INSERT INTO todo.tasks (description) VALUES (?)",[description]) +def add_task(cur, description): + cur.execute("INSERT INTO demo.tasks (description) VALUES (?)",[description]) print(f"Task (id={cur.lastrowid}) added successfully") -def updateTask(cur, description, id): - cur.execute("UPDATE todo.tasks set completed = ? WHERE id = ?",[description,id]) +def update_task(cur, description, id): + cur.execute("UPDATE demo.tasks set completed = ? WHERE id = ?",[description,id]) print(f"Task (id={id}) status updated") -def showTasks(cur): - cur.execute("SELECT * FROM todo.tasks") +def show_tasks(cur): + cur.execute("SELECT * FROM demo.tasks") # Print the results stored in the cursor for id, description, completed in cur: print(f"id = {id}, description = {description}, completed = {completed}") -def deleteTask(cur, id): - cur.execute("DELETE FROM todo.tasks WHERE id = ?",[id]) +def delete_task(cur, id): + cur.execute("DELETE FROM demo.tasks WHERE id = ?",[id]) print(f"Task (id={id}) deleted") def main(): @@ -39,38 +39,38 @@ def main(): args = sys.argv[1:] if (len(args) == 0): - raise Exception("Invalid arguments") + raise ValueError("Invalid arguments") action = args[0] conn = mariadb.connect( host="127.0.0.1", - user="root", - password="RootPassword123!", + user="user", + password="Password123!", autocommit=True ) cur = conn.cursor() - if (action == "create"): - createSchema(cur) - elif (action == "drop"): - dropSchema(cur) - elif (action == "add"): - description = args[1] - addTask(cur, description) - elif (action == "updateStatus"): - id = args[1] - completed = args[2] - updateTask(cur, id, completed) - elif (action == "show"): - showTasks(cur) - elif (action == "delete"): - id = args[1] - deleteTask(cur, id) - else: - raise Exception("Invalid action argument") - + match action: + case "create": + create_schema(cur) + case "drop": + drop_schema(cur) + case "add": + description = args[1] + add_task(cur, description) + case "updateStatus": + task_id = args[1] + completed = args[2] + update_task(cur, task_id, completed) + case "show": + show_tasks(cur) + case "delete": + task_id = args[1] + delete_task(cur, task_id) + case _: + raise ValueError(f"Invalid action argument: {action}") except Exception as e: print(e) finally: