Best method to query an MYSQL database and use the results of the query as a variable in a Python file? [duplicate]

By default, fetchone returns a tuple with the data from your database. As it currently stands, you need to access your data by index

temp = data[0]

If you want to access your data by the temperature key, you need to use specify your cursor

from mysql.connector.cursor import MySQLCursorDict
...

mycursor = db.cursor(cursor_class=MySQLCursorDict)

...

temp = data['temperature']

Your object data is a tuple and can't be referenced like that. You need to use this:

temp = data[0]