比较 mysql 中 python 的结果值未被采用
comparing mysql result value in python not taken
我目前正在尝试比较 python
中设置为 mysql db 的值
下面是我的代码片段。
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")
所以,结果打印得很好,但我的条件不工作。
看起来结果的值在比较完成时被视为值。
如有任何指导,我们将不胜感激。
谢谢你们。
乔.
万一它可能对其他人有帮助..
我通过指定检索到的值的类型让操作员工作。
我更改了以下内容:
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")
无论如何谢谢你。
乔.
我目前正在尝试比较 python
中设置为 mysql db 的值下面是我的代码片段。
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")
所以,结果打印得很好,但我的条件不工作。 看起来结果的值在比较完成时被视为值。
如有任何指导,我们将不胜感激。 谢谢你们。 乔.
万一它可能对其他人有帮助.. 我通过指定检索到的值的类型让操作员工作。
我更改了以下内容:
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")
无论如何谢谢你。 乔.