我如何在运行python程序时定期查询MySQL数据库?

时间:2019-06-28 14:16:30

标签: python mysql

我有一个脚本,可从本地MySQL数据库中获取值,并使用这些值来更改某些LED上的颜色。用户可以通过网站来获取这些值。

现在,当我运行此脚本时,它将按预期从数据库查询当前值。但是当我以任何方式更新这些值时,该脚本保留的是不使用新值,而不是第一次运行时查询的旧值。

该脚本每30秒检查2个条件,然后执行一个功能,然后根据条件打开或关闭某些LED。

我尝试运行for循环,在该循环的每次迭代结束时查询LEDS的数据,希望它可以再次执行查询,获取更新后的值,然后使用新值进行重申。

def getLedsByLightID( id ):
    conn = mydb.cursor()
    sql = ("SELECT * FROM leds WHERE traffic_light_id =" + str(id) )
    conn.execute( sql )
    return conn.fetchall()
for x in getLights(): 
    leds = getLedsByLightID(x[0]) 
    ledsList = [leds]
    trafficList = [x]
    trafficList.append(ledsList)
    relayIpAddress = trafficList[0][3]
    relayIpAddressPort = trafficList[0][4]

EDIT: getLights() queries the db and fetches the actual lamp object the LEDs from getLedsByLightID(id) are joined to. The values fetched from that table are - id, name, location(physical), Ip adress, ip address port. - these values correspend with trafficList[0] - [4]  respectively.

while True:
    for e in getLights():
        if checkHost(relayIpAddress, relayIpAddressPort) and checkHost(prtgUrl, prtgUrlPort):
            for number in range(3):
                xmldoc = minidom.parse(urlopen(prtgApiUrl))  # parse PRTG date from url
                for number in range(1):
                    if prtgSensor('downsens') > 0:
                        for z in trafficList:
                            for h in trafficList[1][0]:
                                if str(h[2]) == red:
                                    relaySwitch(str(h[3]), ON)
                                    print(str(h[3]))

                                elif str(h[2]) == green:
                                    relaySwitch(str(h[3]), OFF)
                                    print(str(h[3]))

                    else:
                        for q in trafficList:
                            for u in trafficList[1][0]:
                                if str(u[2]) == red:
                                    relaySwitch(str(u[3]), OFF)
                                    print(str(u[3]))

                                elif str(u[2]) == green:
                                    relaySwitch(str(u[3]), ON)
                                    print(str(u[3]))

                    if prtgSensor("warnsens") > 0:
                        for j in trafficList:
                            for k in trafficList[1][0]:
                                if str(k[2]) == orange:
                                    relaySwitch(str(k[3]), ON)
                                    print(str(k[3]))

                    else:
                        for t in trafficList:
                            for c in trafficList[1][0]:
                                if str(k[2]) == orange:
                                    relaySwitch(str(k[3]), OFF)
                                    print(str(k[3]))

                # wait for 30 seconds
                time.sleep( 30 )
                print("refresh")
              ```This is where I tried to run the query that would get the new 
                 values```
            print ('Host(s) are Up and running!')
        else:
            print ('host(s) are down')
            time.sleep( 60 )

我希望在我评论的地方再次运行查询后,将更新“ Example [3]”的值,然后可以通过检查那里的内容进行确认。但是什么都没有改变。我不知道该怎么办。

0 个答案:

没有答案