用python/flask实现mysql查询

mhd8tkvw  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(614)

刚刚回到一个停滞的项目。我正在学习python(使用flask)和一个使用mysql的脚本。
我在一个树莓皮上运行了一些python,它可以检测蓝牙,看有没有人进来或者出去。这会写入mysql表并正常工作。
我正在尝试读回行,目前正在使用以下命令:

conn = MySQLdb.connect(host="localhost", user = "xxxxxxx", passwd = "xxxxxxxx", db = "mydb")
                cursor = conn.cursor()
                cursor.execute("select status from occupants WHERE id = '1'")
                data = cursor.fetchall()
                conn.commit()
                if (data[0] == "In"):
                    result = "In"

在我的模板文件中,我有:

{% if result == "In" %}
   Do stuff
{% else %}
   Do other stuff

目前的结果总是“无”。。。可能是因为:

def index(iframe=None, result=None, targettemp=None, status=None, inttemp=None, result1=None, result2=None, result3=None, hiveSessionId=None):

我已经做了大量的搜索,但我甚至不知道我是否在寻找正确的东西。
这是一个可怕的错误以至于不值得挽救,还是一个简单的错误?
编辑:这是我的python脚本中的整个路线:
我改变了一点,就是把结果设为1。这只是测试它们是否被传递到模板,而模板是有效的。我还为结果添加了一个else。所以现在当我看我的输出时,它显示“blah”,这证明了这个结果!=“在“尽管我可以看到它在mysql表中。

@app.route('/')
def index(iframe=None, result=None, targettemp=None, status=None, inttemp=None, result1=None, result2=None, result3=None, hiveSessionId=None):
        import requests
        import bluetooth
        import time
        import datetime
        import MySQLdb
    iframe = "http://xx.xx.xx.xx:xxxx/cam/min.php"
    url = "https://api.prod.bgchprod.info:443/omnia/users"
        if 'hiveSessionId' in session:
                hiveSessionId = session['hiveSessionId']
                headers = {
                    'Content-Type': "application/vnd.alertme.zoo-6.1+json",
                    'Accept': "application/vnd.alertme.zoo-6.1+json",
                    'X-Omnia-Client': "Hive Web Dashboard",
                    'X-Omnia-Access-Token': hiveSessionId,
                    'Cache-Control': "no-cache"
                    }
                response = requests.request("GET", url, headers=headers)
                data=response.json()
                if 'errors' in data:
                        return redirect(url_for('hivelogin'))
                conn = MySQLdb.connect(host="localhost", user = "xxxxx", passwd = "xxxxxx", db = "mydb")
                cursor = conn.cursor()
                cursor.execute("select status from occupants WHERE id = '1'")
                data = cursor.fetchall()
                conn.commit()
                if (data[0] == "In"):
                    result = "In"
                else:
                    result = "Blah"
                result1 = 1
                result2 = 1
                result3 = 1

        url = "https://api-prod.bgchprod.info:443/omnia/nodes/0e5f20fb-ab13-4d43-89ed-ec2481ea9012"
                payload = "{\n    \"nodes\": [{\n        \"attributes\": {\n            \"state\": {\"targetValue\": \"OFF\"}\n        }\n    }]\n}"
                headers = {
                'Content-Type': "application/vnd.alertme.zoo-6.1+json",
                'Accept': "application/vnd.alertme.zoo-6.1+json",
                'X-Omnia-Client': "Hive Web Dashboard",
                'X-Omnia-Access-Token': hiveSessionId,
                'Cache-Control': "no-cache",
        }
                responsetemp = requests.request("PUT", url, data=payload, headers=headers)
                data=responsetemp.json()
                inttemp  = (data['nodes'][0]['attributes']['temperature']['reportedValue'])
                inttemp = round(inttemp,1)
        targettemp  = (data['nodes'][0]['attributes']['targetHeatTemperature']['reportedValue'])
        status  = (data['nodes'][0]['attributes']['stateHeatingRelay']['reportedValue'])
        return render_template('inout.html', iframe=iframe, status=status, targettemp=targettemp, inttemp=inttemp, hiveSessionId=hiveSessionId, result=result, result1=result1, result2=result2, result3=result3)
        return redirect(url_for('hivelogin'))
xmq68pz9

xmq68pz91#

tuple从不等于string。这就是为什么你 resultBlah . con.commit()是不必要的,因为在一般情况下,查询不需要提交。但如果需要禁用查询缓存。如何使用mysql.connector禁用查询缓存

cur.execute("select msg from test limit 2")
data = cur.fetchall()
print(data)  # (('test',), ('test',))
if (data[0] == "test"): #data[0] ('test',)
    result = "In"
else:
    result = "Blah"
print(result) # Blah

con.commit()

相关问题