在python中比较mysql结果值未被采用
我目前正在尝试将值集与python中的mysql db进行比较 下面是我的代码片段在python中比较mysql结果值未被采用,python,mysql,variables,types,comparator,Python,Mysql,Variables,Types,Comparator,我目前正在尝试将值集与python中的mysql db进行比较 下面是我的代码片段 def query(): # CONNECTION TO THE DB. connection = pymysql.connect(host=host, user=user,
def query():
# CONNECTION TO THE DB.
connection = pymysql.connect(host=host,
user=user,
password=pass1,
db=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
logf.write(dateandtime + "- 3 - Getting the sensor value\n")
# Fetch the sensor name
sql = "SELECT temp FROM expected_temp WHERE name = (select name from sensor where id = %s)"
cursor.execute(sql, (sens,))
result = cursor.fetchall()
for row in result:
na = "%s" % (row["temp"])
nam = str(na)
print (nam)
if __name__ == '__main__':
#Setup the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
try:
while True:
humidity, temperature = readAdafruitDHT('2302',17)
logf.write(dateandtime + "- 2 - Reading temperature\n")
target = query()
if temperature > target:
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to HIGH')
logf.write(dateandtime + ' - Changing to HIGH\n')
GPIO.output(18,GPIO.HIGH)
else:
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to LOW')
logf.write(dateandtime + ' - Changing to LOW\n')
GPIO.output(18,GPIO.LOW)
time.sleep(20)
except KeyboardInterrupt:
GPIO.cleanup()
print("Bye")
因此,结果打印得很好,但我的条件不起作用。
当比较完成时,其结果值似乎被视为值
任何指导都将不胜感激。
谢谢大家。
Joe.以防对其他人有所帮助。。 通过指定检索到的值的类型,我让运算符工作 我更改了以下内容:
def query():
# CONNECTION TO THE DB.
connection = pymysql.connect(host=host,
user=user,
password=pass1,
db=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
logf.write(dateandtime + "- 3 - Getting the sensor value\n")
# Fetch the sensor name
sql = "SELECT temp FROM expected_temp WHERE name = (select name from sensor where id = %s)"
cursor.execute(sql, (sens,))
result = cursor.fetchall()
for row in result:
na = "%s" % (row["temp"])
nam = str(na)
#nam = float(na)
#n = nam.rstrip()
return int(nam)
if __name__ == '__main__':
#Setup the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
try:
while True:
humidity, temperature = readAdafruitDHT('2302',17)
logf.write(dateandtime + "- 2 - Reading temperature\n")
target = query()
print target
print (dateandtime, target)
if ( temperature > target ):
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to HIGH')
logf.write(dateandtime + ' - Changing to HIGH\n')
GPIO.output(18,GPIO.HIGH)
else:
print (dateandtime + ' - Current temperature: %f' % temperature)
logf.write(dateandtime + " - 3 - Current temperature: %f" % temperature + "\n")
print (dateandtime + ' - Changing to LOW')
logf.write(dateandtime + ' - Changing to LOW\n')
GPIO.output(18,GPIO.LOW)
time.sleep(20)
except KeyboardInterrupt:
GPIO.cleanup()
print("Bye")
无论如何谢谢你。
Joe。请修复您的缩进。和后可运行代码;这将失败,因为您尝试时没有例外。您好,我添加了例外。代码运行正常-除了if temperature>target。。。。