I have a simple python/flask api that I am unable to match an input in the api to mysql select results:
mycursor = mydb.cursor()
mycursor.execute('SELECT code FROM users')
codes = mycursor.fetchall()
codes prints like so '[('0000',), ('0001',), ('0003',)]'
Here is the api route and lookup code:
# API route for when a code is passed
@app.route('/api/v1/codes', methods=['GET'])
def checkcode():
# Check if a code was provided as part of the URL.
# If code is provided, assign it to a variable.
# If no code is provided, display an error in the browser.
if 'id' in request.args:
code = request.args['id']
print(id)
else:
return "Error: No id field provided. Please specify an id."
# Check if code matches
if code in codes:
#unlock(),
pushMessage("Message from PiAPI", "Building Door Unlocked.")
else:
wrong = 'Wrong Code was sent ' + (code)
pushMessage('Message from PiAPI', wrong)
I've tried getting the code to match in every way I can conceive like so:
code = "('" + request.args['id'] + "',)"
code = "'" + request.args['id'] + "',"
code = "'" + request.args['id'] + "'"
Nothing has matched so far so even though it looks correct it and gets sent correctly in the else: pushmessage. In the api I'm entering 0000 (/api/v1/codes?id=0000)
I'm 42 and brand new to coding so please forgive any stupidity. :)
codes = [record[0] for record in codes], and the rest of the route should work as you expect.