python - 使用 Python/Flask 进行 MySQL 查询
问题描述
刚回到一个停滞的项目。我正在学习 Python(使用 Flask)并使用一个也使用了一些 MySQL 的脚本。
我在 Raspberry Pi 上运行了一些 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”,这证明了结果!=“In”,即使我可以看到它在 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'))
解决方案
元组永远不等于字符串。result
这就是你的原因Blah
。而 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()
推荐阅读
- java - 结果集显示为空,但表中至少有 1 条记录
- .net - 使用 PrivateFontCollection 的 AddMemoryFont() 优于 AddFontFile() 有什么好处
- angular - Angular & Spring - 标头未传递到后端
- css - 动态网格布局填充最后一行
- python - 如何使用python一一运行pip的命令行
- go - 无法通过 net/http 调用获取整个标头元素
- bash - 如何在while循环中使用curl的值?
- r - 在合并 r 中的两个数据帧时计算变化
- reactjs - React - 登录后HeaderComponent不会改变?
- java - Java:在流行的 IDE 中是否有重构“将整个实体传递给方法而不是实体属性”?