Skip to content

Instantly share code, notes, and snippets.

@dpnkrg
Created September 14, 2018 06:46
Show Gist options
  • Save dpnkrg/10840a5d8bb28c1fa61b20b0e2010a91 to your computer and use it in GitHub Desktop.
Save dpnkrg/10840a5d8bb28c1fa61b20b0e2010a91 to your computer and use it in GitHub Desktop.
import os
from flask import Flask
from flask.ext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
mysql_database_host = 'MYSQL_DATABASE_HOST' in os.environ and os.environ['MYSQL_DATABASE_HOST'] or 'localhost'
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'db_user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd'
app.config['MYSQL_DATABASE_DB'] = 'employee_db'
app.config['MYSQL_DATABASE_HOST'] = mysql_database_host
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
@app.route("/")
def main():
return "Welcome!"
@app.route('/how are you')
def hello():
return 'I am good, how about you?'
@app.route('/read from database')
def read():
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
result = []
while row is not None:
result.append(row[0])
row = cursor.fetchone()
return ",".join(result)
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment