使用python

时间:2019-01-18 15:16:01

标签: python sql mariadb raspbian

我正在尝试使用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

预先感谢

EDIT 在表条目的示例中添加帖子:

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 收集数据行,然后在一分钟后,当代码中的循环再次运行时,它将打印 pi_id:5768 中的数据,而不是最新数据 pi_id:5769 ,这就是我的意思试图实现。

2 个答案:

答案 0 :(得分: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传达了这个想法。

现在只是将所有内容整理整齐,这样就不会那么令人费解/重复。

谢谢

答案 1 :(得分:0)

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

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

这将运行一次查询并返回字典列表,您可以使用该字典通过键访问值。就目前而言,由于您有LIMIT 1,所以实际上不需要迭代行。