The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Mysql conn
By Guest on 9th January 2019 07:28:23 PM | Syntax: TEXT | Views: 2



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. # from flask import Flask
  2. from flask import Flask,request,jsonify
  3. from numpy import asarray
  4. from flaskext.mysql import MySQL
  5.  
  6. mysql = MySQL()
  7. app = Flask(__name__)
  8. app.config['MYSQL_DATABASE_USER'] = 'root'
  9. app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
  10. app.config['MYSQL_DATABASE_DB'] = 'testdb'
  11. app.config['MYSQL_DATABASE_HOST'] = 'localhost'
  12. mysql.init_app(app)
  13.  
  14.  
  15. def dictify(a):
  16.     return [dict(zip(("id","name"),vv)) for vv in a]
  17.  
  18. @app.route("/")
  19. def hello():
  20.     return "Welcome to Python Flask App!"
  21.  
  22. # http://127.0.0.1:5000/Authenticate?UserName=1&Password=test
  23. @app.route("/Authenticate")
  24. def Authenticate():
  25.     username = request.args.get('UserName')
  26.     password = request.args.get('Password')
  27.     cursor = mysql.connect().cursor()
  28.     cursor.execute("SELECT * from first where id='" + username + "' and description='" + password + "'")
  29.     data = cursor.fetchone()
  30.     if data is None:
  31.      return "Username or Password is wrong"
  32.     else:
  33.      return "Logged in successfully"
  34.  
  35. @app.route("/AllUsers")
  36. def allUsers():
  37.     dbconn = mysql.connect()
  38.     cursor = dbconn.cursor()
  39.     cursor.execute("SELECT * from first")
  40.     print(cursor)
  41.     data = cursor.fetchall()
  42.     if data is None:
  43.         return "Bad Request"
  44.     else:
  45.         return(jsonify(dictify(asarray(data))))
  46.    
  47.     cursor.close()
  48.     dbconn.close()
  49.  
  50.  
  51. if __name__ == "__main__":
  52.     app.run()



  • Recent Pastes
  • Hhh
    7 mins 31 secs ago
  • Pyrup
    1 hour 5 mins 48 secs ago
  • 123
    1 hour 57 mins 39 secs ago
  • Wdw
    3 hours 52 mins 17 secs ago
  • Flame test Ans
    3 hours 52 mins 31 secs ago