Skip to content

Commit 0c5e201

Browse files
committed
Initial additions
1 parent eb9b947 commit 0c5e201

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
11
# Quick Start: Python and MariaDB
2+
3+
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).
4+
5+
## Requirements
6+
7+
This sample requires the following to be installed/enabled on your machine:
8+
9+
* [Python (v. 3+)](https://www.python.org/downloads/)
10+
* [MariaDB Connector/C (v. 3.1.5+)](https://mariadb.com/products/skysql/docs/clients/mariadb-connector-c-for-skysql-services/) (used by Connector/Python, find more information [here](https://mariadb.com/docs/clients/mariadb-connectors/connector-python/install/))
11+
12+
## Installing MariaDB Connector/Python
13+
14+
To run the sample code in this repository you'll first need to [install the MariaDB Python connector (driver)](https://mariadb.com/docs/clients/mariadb-connectors/connector-python/install/).
15+
16+
## Running these samples
17+
18+
Once you've installed the MariaDB Python connector you're ready to run the [tasks.py](src/tasks.py) sample.
19+
20+
The [tasks.py](src/tasks.py) sample can be used to:
21+
22+
* `Create` a database and table (necessary for the subsequent CRUD operations).
23+
24+
```bash
25+
$ python3 tasks.py create
26+
```
27+
28+
* `Drop` the database (and table).
29+
30+
```bash
31+
$ python3 tasks.py drop
32+
```
33+
34+
* `Insert` a new tasks record.
35+
36+
```bash
37+
$ python3 tasks.py add 'New Task Description'
38+
```
39+
40+
* `Update` a task record's completion field (by specifying the `id` and `completion` value).
41+
42+
```bash
43+
$ python3 tasks.py update 3 1
44+
```
45+
46+
* `Select` and print all tasks.
47+
48+
```bash
49+
$ python3 tasks.py show
50+
```
51+
52+
* `Delete` a task record (by `id`).
53+
54+
```bash
55+
$ python3 tasks.py delete 3
56+
```
57+
58+
## Helpful Resources
59+
60+
* [How to connect Python programs to MariaDB](https://mariadb.com/resources/blog/how-to-connect-python-programs-to-mariadb/) (blog)
61+
* [Official MariaDB Documentation](https://mariadb.com/docs)
62+
* [MariaDB Quickstart Guide](https://github.com/mariadb-developers/mariadb-getting-started)
63+
64+
## Support and Contribution
65+
66+
Please feel free to submit PR's, issues or requests to this project directly.
67+
68+
If you have any other questions, comments, or looking for more information on MariaDB please check out:
69+
70+
* [MariaDB Developer Hub](https://mariadb.com/developers)
71+
* [MariaDB Community Slack](https://r.mariadb.com/join-community-slack)
72+
73+
Or reach out to us directly via:
74+
75+
76+
* [MariaDB Twitter](https://twitter.com/mariadb)
77+
78+
## License <a name="license"></a>
79+
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=plastic)](https://opensource.org/licenses/MIT)

src/tasks.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import sys
2+
import mariadb
3+
4+
def createSchema(cur):
5+
cur.execute("CREATE DATABASE todo")
6+
print("todo database created")
7+
cur.execute("""CREATE TABLE todo.tasks (
8+
id INT(11) unsigned NOT NULL AUTO_INCREMENT,
9+
description VARCHAR(500) NOT NULL,
10+
completed BOOLEAN NOT NULL DEFAULT 0,
11+
PRIMARY KEY (id)
12+
)""")
13+
print("tasks table created")
14+
15+
def dropSchema(cur):
16+
cur.execute("DROP DATABASE todo")
17+
print("todo database and tasks table dropped")
18+
19+
def addTask(cur, description):
20+
cur.execute("INSERT INTO todo.tasks (description) VALUES (?)",[description])
21+
print(f"Task (id={cur.lastrowid}) added successfully")
22+
23+
def updateTask(cur, description, id):
24+
cur.execute("UPDATE todo.tasks set completed = ? WHERE id = ?",[description,id])
25+
print(f"Task (id={id}) status updated")
26+
27+
def showTasks(cur):
28+
cur.execute("SELECT * FROM todo.tasks")
29+
# Print the results stored in the cursor
30+
for id, description, completed in cur:
31+
print(f"id = {id}, description = {description}, completed = {completed}")
32+
33+
def deleteTask(cur, id):
34+
cur.execute("DELETE FROM todo.tasks WHERE id = ?",[id])
35+
print(f"Task (id={id}) deleted")
36+
37+
def main():
38+
try:
39+
args = sys.argv[1:]
40+
41+
if (len(args) == 0):
42+
raise Exception("Invalid arguments")
43+
44+
action = args[0]
45+
46+
conn = mariadb.connect(
47+
host="127.0.0.1",
48+
user="root",
49+
password="RootPassword123!",
50+
autocommit=True
51+
)
52+
53+
cur = conn.cursor()
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+
74+
except Exception as e:
75+
print(e)
76+
finally:
77+
conn.close()
78+
79+
if __name__ == "__main__":
80+
main()

0 commit comments

Comments
 (0)