igdb/Code/database/db_setup.py

50 lines
1.2 KiB
Python

from subprocess import call
from sys import argv
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from fields import Glacier, Annual_Data, Annual_Change, User
def create_database(db_name, db_user, db_pw):
script = "mariadb_setup.sh"
output = call([script + db_name + db_user + db_pw])
if output != 0:
print("Error: couldn't create database")
exit()
def create_connection(db_name, db_user, db_pw):
host = "localhost:3306"
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
user=db_user, pw=db_pw, url=host, db=db_name
)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = connection_uri
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
global db
db = SQLAlchemy(app)
def create_tables(db):
db.create_all()
def main():
if len(argv) != 3:
print(
"Usage: " + argv[0] + " <database name> <database user> <database password>"
)
exit()
db_name = str(argv[1])
db_user = str(argv[2])
db_password = str(argv[3])
create_database(db_name, db_user, db_password)
create_connection(db_name, db_user, db_password)
create_tables(db)
if __name__ == "__main__":
main()