|
1 | 1 | import sys
|
2 | 2 | import mariadb
|
3 | 3 |
|
4 |
| -def createSchema(cur): |
5 |
| - cur.execute("CREATE DATABASE todo") |
6 |
| - print("todo database created") |
7 |
| - cur.execute("""CREATE TABLE todo.tasks ( |
| 4 | +def create_schema(cur): |
| 5 | + cur.execute("CREATE DATABASE demo") |
| 6 | + print("demo database created") |
| 7 | + cur.execute("""CREATE TABLE demo.tasks ( |
8 | 8 | id INT(11) unsigned NOT NULL AUTO_INCREMENT,
|
9 | 9 | description VARCHAR(500) NOT NULL,
|
10 | 10 | completed BOOLEAN NOT NULL DEFAULT 0,
|
11 | 11 | PRIMARY KEY (id)
|
12 | 12 | )""")
|
13 | 13 | print("tasks table created")
|
14 | 14 |
|
15 |
| -def dropSchema(cur): |
16 |
| - cur.execute("DROP DATABASE todo") |
17 |
| - print("todo database and tasks table dropped") |
| 15 | +def drop_schema(cur): |
| 16 | + cur.execute("DROP DATABASE demo") |
| 17 | + print("demo database and tasks table dropped") |
18 | 18 |
|
19 |
| -def addTask(cur, description): |
20 |
| - cur.execute("INSERT INTO todo.tasks (description) VALUES (?)",[description]) |
| 19 | +def add_task(cur, description): |
| 20 | + cur.execute("INSERT INTO demo.tasks (description) VALUES (?)",[description]) |
21 | 21 | print(f"Task (id={cur.lastrowid}) added successfully")
|
22 | 22 |
|
23 |
| -def updateTask(cur, description, id): |
24 |
| - cur.execute("UPDATE todo.tasks set completed = ? WHERE id = ?",[description,id]) |
| 23 | +def update_task(cur, description, id): |
| 24 | + cur.execute("UPDATE demo.tasks set completed = ? WHERE id = ?",[description,id]) |
25 | 25 | print(f"Task (id={id}) status updated")
|
26 | 26 |
|
27 |
| -def showTasks(cur): |
28 |
| - cur.execute("SELECT * FROM todo.tasks") |
| 27 | +def show_tasks(cur): |
| 28 | + cur.execute("SELECT * FROM demo.tasks") |
29 | 29 | # Print the results stored in the cursor
|
30 | 30 | for id, description, completed in cur:
|
31 | 31 | print(f"id = {id}, description = {description}, completed = {completed}")
|
32 | 32 |
|
33 |
| -def deleteTask(cur, id): |
34 |
| - cur.execute("DELETE FROM todo.tasks WHERE id = ?",[id]) |
| 33 | +def delete_task(cur, id): |
| 34 | + cur.execute("DELETE FROM demo.tasks WHERE id = ?",[id]) |
35 | 35 | print(f"Task (id={id}) deleted")
|
36 | 36 |
|
37 | 37 | def main():
|
38 | 38 | try:
|
39 | 39 | args = sys.argv[1:]
|
40 | 40 |
|
41 | 41 | if (len(args) == 0):
|
42 |
| - raise Exception("Invalid arguments") |
| 42 | + raise ValueError("Invalid arguments") |
43 | 43 |
|
44 | 44 | action = args[0]
|
45 | 45 |
|
46 | 46 | conn = mariadb.connect(
|
47 | 47 | host="127.0.0.1",
|
48 |
| - user="root", |
49 |
| - password="RootPassword123!", |
| 48 | + user="user", |
| 49 | + password="Password123!", |
50 | 50 | autocommit=True
|
51 | 51 | )
|
52 | 52 |
|
53 | 53 | cur = conn.cursor()
|
54 | 54 |
|
55 |
| - if (action == "create"): |
56 |
| - createSchema(cur) |
57 |
| - elif (action == "drop"): |
58 |
| - dropSchema(cur) |
59 |
| - elif (action == "add"): |
60 |
| - description = args[1] |
61 |
| - addTask(cur, description) |
62 |
| - elif (action == "updateStatus"): |
63 |
| - id = args[1] |
64 |
| - completed = args[2] |
65 |
| - updateTask(cur, id, completed) |
66 |
| - elif (action == "show"): |
67 |
| - showTasks(cur) |
68 |
| - elif (action == "delete"): |
69 |
| - id = args[1] |
70 |
| - deleteTask(cur, id) |
71 |
| - else: |
72 |
| - raise Exception("Invalid action argument") |
73 |
| - |
| 55 | + match action: |
| 56 | + case "create": |
| 57 | + create_schema(cur) |
| 58 | + case "drop": |
| 59 | + drop_schema(cur) |
| 60 | + case "add": |
| 61 | + description = args[1] |
| 62 | + add_task(cur, description) |
| 63 | + case "updateStatus": |
| 64 | + task_id = args[1] |
| 65 | + completed = args[2] |
| 66 | + update_task(cur, task_id, completed) |
| 67 | + case "show": |
| 68 | + show_tasks(cur) |
| 69 | + case "delete": |
| 70 | + task_id = args[1] |
| 71 | + delete_task(cur, task_id) |
| 72 | + case _: |
| 73 | + raise ValueError(f"Invalid action argument: {action}") |
74 | 74 | except Exception as e:
|
75 | 75 | print(e)
|
76 | 76 | finally:
|
|
0 commit comments