Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. # MySQL in PythonAnywhere with Flask -SQLAlchemy.py
  2. # A very simple Flask Hello World app for you to get started with...
  3.  
  4. from flask import Flask
  5. from flask_sqlalchemy import SQLAlchemy
  6. # import MySQLdb
  7.  
  8. app = Flask(__name__)
  9. app.config["DEBUG"] = True
  10.  
  11. user, password = '********', '********'
  12. host = '********.mysql.pythonanywhere-services.com'
  13. db = '********$dbFlask' # dbFlask was created as a PythonAnywhere MySQL database
  14.  
  15. # connection string: mysql://user:pword@host/db
  16. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{0}:{1}@{2}/{3}'.format(user, password, host, db)
  17. db = SQLAlchemy(app)
  18.  
  19. class User(db.Model):
  20. id = db.Column(db.Integer, primary_key=True)
  21. username = db.Column(db.String(20))
  22. email = db.Column(db.String(50))
  23.  
  24. def __init__(self, username, email):
  25. self.username = username
  26. self.email = email
  27.  
  28. def __repr__(self):
  29. return '<User name:%r>' % self.username
  30.  
  31.  
  32. @app.route('/')
  33. def hello_world():
  34. u = User('Fulano', 'fulano@gmail.com')
  35. db.session.add(u)
  36. db.session.commit()
  37. u = User.query.first()
  38. return u.username
  39.  
  40. @app.route('/reload')
  41. def reload():
  42. db.drop_all()
  43. db.create_all()
  44. return 'database (re)created!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement