比较python中的mysql结果值

nle07wnf  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(234)

我目前正在尝试将值集与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")

所以,结果很好打印,但我的条件是不工作。当比较完成时,结果值看起来就是值。
任何指导都将不胜感激。谢谢大家。乔。

gojuced7

gojuced71#

以防对其他人有帮助。。我让操作符工作,通过指定检索到的值的类型。
我更改了以下内容:

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")

无论如何谢谢你。乔。

相关问题