visit
$ docker run -p 3306:3306 -d --name mariadb -eMARIADB_ROOT_PASSWORD=Password123! mariadb/server:10.4
While you can certainly use a variety of other SQL clients, for the sake of keeping things simple and uniform, I've only included samples using the official MariaDB client.Connect to your MariaDB instance by executing the following command in a terminal window.
$ mariadb --host 127.0.0.1 -P 3306 --user root -pPassword123!
CREATE DATABASE demo;
CREATE TABLE demo.people (name VARCHAR(50));
INSERT INTO demo.people VALUES ('rob'), ('tracy'), ('sam'), ('duke');
$ pyvenv venv
Tip: pyvenv is only available in Python 3.4 or later. For older versions please use the tool.
Before you can start installing or using packages in your virtual environment, you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH.
Activate the virtual environment using the following command:$ . venv/bin/activate activate
TL;DR (if I am even allowed to do that for two sentences), it's a great, lightweight framework to use for an API.First, install Flask.
$ pip3 install flask
Then create a new file called api.py that will be used to create a new Flask app, and add the following code:
# import necessary packages
import flask
# create the flask app
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def index():
return 'Success!'
# run the app
app.run()
To create an instance, it has to be give a name. Using (__name__) ensures that it can be started as an application or imported as a module. The use of the route() decorator lets our flask app know which URL should trigger the corresponding method.You're ready to run the app.
$ python3 api.py
$ pip3 install mariadb
Open api.py, and add code to import the MariaDB and packages directly under the Flask package import.
import json
import mariadb
config = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': 'Password123!',
'database': 'demo'
}
The config object will be used within a new route method block to connect to MariaDB.
# route to return all people
@app.route('/api/people', methods=['GET'])
def index():
# connection for MariaDB
conn = mariadb.connect(**config)
# create a connection cursor
cur = conn.cursor()
# execute a SQL statement
cur.execute("select * from people")
# serialize results into JSON
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for result in rv:
json_data.append(dict(zip(row_headers,result)))
# return the results!
return json.dumps(json_data)
Bringing it all together, your api.py content should look like the following:
# import the necessary packages
import flask
import json
import mariadb
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# configuration used to connect to MariaDB
config = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': 'Password123!',
'database': 'demo'
}
# route to return all people
@app.route('/api/people', methods=['GET'])
def index():
# connection for MariaDB
conn = mariadb.connect(**config)
# create a connection cursor
cur = conn.cursor()
# execute a SQL statement
cur.execute("select * from people")
# serialize results into JSON
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for result in rv:
json_data.append(dict(zip(row_headers,result)))
# return the results!
return json.dumps(json_data)
app.run()
$ python3 api.py
Then test the people endpoint. This can be done through a variety of techniques (e.g. directly within a browser, , etc.).
For instance, considering using a command:$ curl //localhost:5000/api/people
[{"name":"rob"},{"name":"tracy"},{"name":"duke"},{"name":"sam"}]