Semester-Database Project
We use pip, python's package manager, to install each individual Python package (library) directly for the entire system, or create a virtual environment with the venv
module.
The necessary packages for this app are listed in requirements.txt
and can be installed all together via pip install -r requirements.txt
.
In order to send queries to a database from a Python program, a connection between it and the databases' server must be established first. That is accomplished by a cursor object from the Flask-MySQLdb
library, and using the appropriate methods (execute
, commit
).
-
Never upload passwords or API keys to github. One simple way to secure your passwords is to store them in a separate file, that will be included in
.gitignore
: We import the credentials in__init__.py
by replacing theapp.config
commands with: dbdemo/config.json{ "MYSQL_USER": "root", "MYSQL_PASSWORD": "", "MYSQL_DB": "elidek", "MYSQL_HOST": "localhost", "SECRET_KEY": "key", "WTF_CSRF_SECRET_KEY": "key" }
import json ## ... app.config.from_file("config.json", load = json.load)
__init__.py
configures the application, including the necessary information and credentials for the database
routes.py
currently contains all the endpoints and corresponding controllers
run.py
launches the simple, built-in server and runs the app on it
In order to run we can use the flask run
command (if we set the environment variable FLASK_APP
to run.py
) or directly with run.py
.
We also include the DDL and DML scripts that are used to generate the MySQL database and its data. We also include a queries.sql file including most of the queries used.