代码之家  ›  专栏  ›  技术社区  ›  Joe

在python中比较mysql结果值未采用

  •  -1
  • Joe  · 技术社区  · 6 年前

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

    因此,结果打印得很好,但我的状况不起作用。 看起来,在进行比较时,结果值被视为值。

    如有任何指导,将不胜感激。 谢谢大家。 乔。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Joe    6 年前

    以防对其他人有帮助。。 我让操作符通过指定检索到的值的类型来工作。

    我已更改以下内容:

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

    无论如何,谢谢你。 乔。