Home comparing mysql result value in python not taken
Reply: 0

comparing mysql result value in python not taken

user2949
1#
user2949 Published in April 24, 2018, 10:28 am

I am currently trying to compare value set to mysql db in python

below is a snippet of my code.

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

So, the result is well printed however my condition is not working. Its look like that the value as a result is seen as value when the comparison is done.

Any guidance would be appreciated. Thank you all. Joe.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.372337 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO