neutrino
neutrino

Reputation: 924

how to set SQLALCHEMY_DATABASE_URI?

I have a simple React app on the frontend, being served by Flask in the backend. I also have my MySQLWorkbench open; the db is running and I've been able to modify it directly in mysqlworkbench.

In main.py, I have to set the SQLAlchemy variable, but I'm getting the following error:

File "main.py", line 10, in <module>

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://"+ os.environ['Administrator'] + ":" + os.environ['Photon959']+ "@" + os.environ['pcadata.<>.rds.amazonaws.com'] + ":3306/innodb"
File "/Users/<>/miniconda3/lib/python3.7/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'Administrator'

here is my main.py, where I should be setting the SQLAlchemy variables.

from flask import (Flask, request, render_template)
from flask_mysqldb import MySQL
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask("__main__")

mysql = SQLAlchemy(app)

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://"+ 
os.environ['Administrator'] + ":" + os.environ[<>]+ 
"@" + os.environ['<>.rds.amazonaws.com'] + 
":3306/innodb"

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        details = request.form
        firstName = details['fname']
        lastName = details['lname']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO MyUsers(firstName, lastName) VALUES (%s, %s)", (firstName, lastName))
        db.connection.commit()
        cur.close()
        return 'success'
    return render_template('index.html')

app.run(debug=True)

In that main.py, I'm setting the DB user to Administrator (when I'm logged in as on AWS). But that error is coming up, so I'm not quite sure where to go from here. Any help is appreciated!

Upvotes: 2

Views: 13096

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55884

There is no "Administrator" environment variable set, so you get a KeyError when accessing os.environ['Administrator'].

os.environ provides a dictionary-style interface to access environment variables. Environment variables are key/value pairs set outside of your program. To get the value of an environment variable from os.environ you use its name, for example:

db_name = os.environ["DB_NAME"]

In the code in the question, it looks as if the code is using the expected values as environment variable names, for example:

os.environ['<>.rds.amazonaws.com']

instead of

os.environ['DB_HOST']

If this app's code is not going to be shared and the configuration isn't going to change, it may be simplest for now to hardcode the details:

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://Administrator:<>@<>.rds.amazonaws.com3306/innodb"

To get the database configuration from the environment, the code would look like this:

app.config['SQLALCHEMY_DATABASE_URI'] = ("mysql+pymysql://"+ os.environ['DB_USER'] + ":" 
                                         + os.environ['DB_PASSWORD']+ "@" 
                                         + os.environ['DB_HOST'] 
                                         + ":3306/innodb")

And you would set the environment variables in your terminal before calling your app:

export FLASK_APP=my_app_name
export DB_USER=Administrator
export DB_PASSWORD=password
export DB_HOST=foo.rds.amazonaws.com
flask run

Upvotes: 4

Related Questions