Skip to content

Commit a5e6365

Browse files
committed
Fix issues and update
1 parent c91b3fd commit a5e6365

File tree

3 files changed

+43
-49
lines changed

3 files changed

+43
-49
lines changed

README.md

+6-12
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
This repository will walk you through the process of quickly getting started with [Node.js](https://www.python.org/) and [MariaDB](https://github.com/mariadb-developers/mariadb-getting-started) using the [MariaDB Python connector](https://github.com/mariadb-corporation/mariadb-connector-python).
44

5-
<p align="center" spacing="10">
6-
<kbd>
7-
<img src="media/demo.gif" />
8-
</kbd>
9-
</p>
10-
115
## Requirements
126

137
This sample requires the following to be installed/enabled on your machine:
@@ -28,37 +22,37 @@ The [tasks.py](src/tasks.py) sample can be used to:
2822
* `Create` a database and table (necessary for the subsequent CRUD operations).
2923

3024
```bash
31-
$ python3 tasks.py create
25+
$ python3 src/tasks.py create
3226
```
3327

3428
* `Drop` the database (and table).
3529

3630
```bash
37-
$ python3 tasks.py drop
31+
$ python3 src/tasks.py drop
3832
```
3933

4034
* `Insert` a new tasks record.
4135

4236
```bash
43-
$ python3 tasks.py add 'New Task Description'
37+
$ python3 src/tasks.py add 'New Task Description'
4438
```
4539

4640
* `Update` a task record's completion field (by specifying the `id` and `completion` value).
4741
4842
```bash
49-
$ python3 tasks.py update 3 1
43+
$ python3 src/tasks.py updateStatus 3 1
5044
```
5145
5246
* `Select` and print all tasks.
5347
5448
```bash
55-
$ python3 tasks.py show
49+
$ python3 src/tasks.py show
5650
```
5751
5852
* `Delete` a task record (by `id`).
5953
6054
```bash
61-
$ python3 tasks.py delete 3
55+
$ python3 src/tasks.py delete 3
6256
```
6357
6458
## Helpful Resources

media/demo.gif

-546 KB
Binary file not shown.

src/tasks.py

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
import sys
22
import mariadb
33

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 (
88
id INT(11) unsigned NOT NULL AUTO_INCREMENT,
99
description VARCHAR(500) NOT NULL,
1010
completed BOOLEAN NOT NULL DEFAULT 0,
1111
PRIMARY KEY (id)
1212
)""")
1313
print("tasks table created")
1414

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")
1818

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])
2121
print(f"Task (id={cur.lastrowid}) added successfully")
2222

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])
2525
print(f"Task (id={id}) status updated")
2626

27-
def showTasks(cur):
28-
cur.execute("SELECT * FROM todo.tasks")
27+
def show_tasks(cur):
28+
cur.execute("SELECT * FROM demo.tasks")
2929
# Print the results stored in the cursor
3030
for id, description, completed in cur:
3131
print(f"id = {id}, description = {description}, completed = {completed}")
3232

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])
3535
print(f"Task (id={id}) deleted")
3636

3737
def main():
3838
try:
3939
args = sys.argv[1:]
4040

4141
if (len(args) == 0):
42-
raise Exception("Invalid arguments")
42+
raise ValueError("Invalid arguments")
4343

4444
action = args[0]
4545

4646
conn = mariadb.connect(
4747
host="127.0.0.1",
48-
user="root",
49-
password="RootPassword123!",
48+
user="user",
49+
password="Password123!",
5050
autocommit=True
5151
)
5252

5353
cur = conn.cursor()
5454

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}")
7474
except Exception as e:
7575
print(e)
7676
finally:

0 commit comments

Comments
 (0)