Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- app = Flask(__name__)
- import pymysql
- ##Connect to the database
- connection = pymysql.connect(host='host.com',
- user='x',
- password='x',
- db='x',
- charset='x',
- cursorclass=pymysql.cursors.DictCursor)
- cur = pymysql.cursors.DictCursor
- @app.route('/', methods=['POST','GET'])
- def index():
- try:
- with connection.cursor() as cursor:
- # Create a new record
- sql = "INSERT INTO `auth` (`dbid`, `keyid`, `secretid`, `lastupdate`) VALUES (%s,%s,%s, %s)"
- cursor.execute(sql, ('44-aaa','1234', 'very-secret', '12-22-09'))
- # connection is not autocommit by default. So you must commit to save
- # your changes.
- connection.commit()
- x = 'success'
- print("success")
- except:
- print("failure")
- return x
- @app.route('/get', methods=['POST','GET'])
- def get():
- try:
- with connection.cursor() as cursor:
- # Read a single record
- sql = "SELECT `secretid` FROM `auth` WHERE `keyid`=%s"
- cursor.execute(sql, ('1234',))
- result = cursor.fetchall()
- solve = str(result)
- finally:
- connection.close()
- return solve
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement