使用 python 从数据库中重复 select 最新数据

Repeatedly select latest Data from Database using python

我正在尝试使用 python 将数据库中的最新数据行存储在变量中,但是脚本存储的是执行时的数据行,而不是最新的数据集。

下面显示了从数据库中提取最后一行的代码。 数据库每分钟获取一行新数据。

#!/usr/bin/env python

#import required modules
from time import sleep
import MySQLdb


#set MySQL Variables
host = "localhost"
user = "user"
password = "pass"
schema = "schema"

#connect to MySQL
db = MySQLdb.connect(host, user, password, schema)
curs = db.cursor()

#Define Sql Queries to Read the last Value in the Database
sqlT="SELECT temperature FROM system_info ORDER BY pi_id DESC LIMIT  1"
sqlH="SELECT humidity FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlIP="SELECT ip_address FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI1="SELECT DI1 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI2="SELECT DI2 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI3="SELECT DI3 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI4="SELECT DI4 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI5="SELECT DI5 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI6="SELECT DI6 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI7="SELECT DI7 FROM system_info ORDER BY pi_id DESC LIMIT 1"
sqlDI8="SELECT DI8 FROM system_info ORDER BY pi_id DESC LIMIT 1"

#Begin Script Infinitely
while True:
        #Gather Values from Database
        curs.execute(sqlT)
        lastT = curs.fetchone()
        for Temp in lastT:
                temp_reading = Temp
        curs.execute(sqlH)
        lastH = curs.fetchone()
        for Hum in lastH:
                hum_reading = Hum
        curs.execute(sqlDI1)
        lastDI1 = curs.fetchone()
        for DI1 in lastDI1:
                DI1_reading = DI1
        curs.execute(sqlDI2)
        lastDI2 = curs.fetchone()
        for DI2 in lastDI2:
                DI2_reading = DI2
        curs.execute(sqlDI3)
        lastDI3 = curs.fetchone()
        for DI3 in lastDI3:
                DI3_reading = DI3
        curs.execute(sqlDI4)
        lastDI4 = curs.fetchone()
        for DI4 in lastDI4:
                DI4_reading = DI4
        curs.execute(sqlDI5)
        lastDI5 = curs.fetchone()
        for DI5 in lastDI5:
                DI5_reading = DI5
        curs.execute(sqlDI1)
        lastDI6 = curs.fetchone()
        for DI6 in lastDI6:
                DI6_reading = DI6
        curs.execute(sqlDI1)
        lastDI7 = curs.fetchone()
        for DI7 in lastDI7:
                DI7_reading = DI7
       curs.execute(sqlDI8)
       lastDI8 = curs.fetchone()
       for DI8 in lastDI8:
                DI8_reading = DI8

        #Print Values
        print(temp_reading)
        print(hum_reading)
        print(DI1_reading)
        print(DI2_reading)
        print(DI3_reading)
        print(DI4_reading)
        print(DI5_reading)
        print(DI6_reading)
        print(DI7_reading)
        print(DI8_reading)

        #Rest so Values change
        sleep(61)

while循环的第一次迭代结果正确显示,但是结果没有更新到数据库中的最新数据集,它已经将行数据存储到变量中一次并且没有更新变量。

我得到的:

22
45
Open
Open
Open
Open
Open
Open
Open
Open
22
45
Open
Open
Open
Open
Open
Open
Open
Open

我想要得到的东西:

22
45
Open
Open
Open
Open
Open
Open
Open
Open
25
56
Open
Open
Closed
Open
Open
Open
Open
Open

提前致谢

编辑 添加 Table 条目的示例以伴随 post:

pi_id | time | ip_address | temperature | humidity | DI1 | DI2 | DI3 | DI4 | DI5 | DI6 | DI7 | DI8 |
5768  | 13:45 | 10.0.0.1 |   22   |   45   | Open | Open | Open | Open | Open | Open | Open | Open |
5769  | 13:46 | 10.0.0.1 |   25   |   56   | Open | Open | Closed | Open | Open | Open | Open | Open |

我 运行 13:45 处的脚本,它收集具有 pi_id 的数据行:5768,然后一分钟后新的一行数据被输入到 table,当代码 运行 中的 While Loop 再次出现时,它会打印来自 的数据pi_id: 5768 而不是最新的数据 pi_id: 5769,这是我正在努力实现的。

我怀疑这不是一个完整的答案,但评论太长了。创建字典列表:

data = []
c.execute("""SELECT * FROM system_info ORDER BY pi_id DE0SC LIMIT 1""")
headers = [item[0] for item in c.description] # List comprehension
for row in c.fetchall():
    data.append([dict(zip(headers, row)])

这将 运行 一次查询和 return 一个字典列表,您可以使用这些字典来按键访问值。就目前而言,没有真正需要迭代行,因为你有一个 LIMIT 1 但这会扩展。

我通过关闭数据库连接然后在 While 循环开始时重新连接来设法获得我需要的结果:

#Begin Script Infinitely
while True:

    #Connect/Re-connect to database
    db = MySQLdb.connect(host, user, password, schema)

    #Define Cursor
    curs = db.cursor()

    #Gather Values from Database
    curs.execute(sqlT)
    lastT = curs.fetchone()
    for Temp in lastT:
        temp_reading = Temp
    curs.execute(sqlH)
    lastH = curs.fetchone()
    for Hum in lastH:
        hum_reading = Hum
    curs.execute(sqlDI1)
    lastDI1 = curs.fetchone()
    for DI1 in lastDI1:
        DI1_reading = DI1       
    curs.execute(sqlDI2)
    lastDI2 = curs.fetchone()
    for DI2 in lastDI2:
        DI2_reading = DI2
    curs.execute(sqlDI3)
    lastDI3 = curs.fetchone()
    for DI3 in lastDI3:
        DI3_reading = DI3
    curs.execute(sqlDI4)
    lastDI4 = curs.fetchone()
    for DI4 in lastDI4:
        DI4_reading = DI4
    curs.execute(sqlDI5)
    lastDI5 = curs.fetchone()
    for DI5 in lastDI5:
        DI5_reading = DI5
    curs.execute(sqlDI6)
    lastDI6 = curs.fetchone()
    for DI6 in lastDI6:
        DI6_reading = DI6
    curs.execute(sqlDI7)
    lastDI7 = curs.fetchone()
    for DI7 in lastDI7:
        DI7_reading = DI7
    curs.execute(sqlDI8)
    lastDI8 = curs.fetchone()
    for DI8 in lastDI8:
        DI8_reading = DI8   

    #Close Cursor
    curs.close()

    #Disconnect from Database
    db.close()

    #Print Values
    print(temp_reading)
    print(hum_reading)
    print(DI1_reading)
    print(DI2_reading)
    print(DI3_reading)
    print(DI4_reading)
    print(DI5_reading)
    print(DI6_reading)
    print(DI7_reading)
    print(DI8_reading)

    #Rest so Values change
    sleep(60)

感谢@Iandru27 和@Rick James 的想法。

现在只是为了整理一下,所以它不是这样 convoluted/repetitive。

谢谢