为什么我的代码从不写入数据库?

2024-09-30 18:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码,对象是读取一行串口,然后添加到数据库,如果它是有效的。发送序列号的代码以\n终止。当这段代码出现在两个脚本中时,它就正常工作了,试图把它推到一起,不知怎么搞砸了。你知道吗

代码如下:

#GKPCM Database Test

#outline open database, read serial,write string, repeat
import serial
import sqlite3

ser = serial.Serial('/dev/pts/2', 19200, timeout=0)
print ser.name          # check which port was really used

db = sqlite3.connect('Data/telemetry.gkpcm')
cursor = db.cursor()

InsertQuery ="""INSERT INTO vehicletelemetry (date,time,cyclecount,rpm,speed,odometer,oiltemp,airtemp,fuellevel,enginetemp,ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"""

tablename="vehicletelemetry"
cursor.execute(""" SELECT COUNT(*) FROM sqlite_master WHERE name = ? """, (tablename, ))
QUERY = cursor.fetchone()
print bool(QUERY[0])    # True if exists
if bool(QUERY[0]) !=1:
    print('not in DB')
    cursor.execute('''CREATE TABLE vehicletelemetry(id INTEGER PRIMARY KEY, date INTEGER,time INTEGER, cyclecount INTEGER, rpm INTEGER, speed INTEGER, odometer INTEGER, oiltemp INTEGER, airtemp INTEGER, fuellevel INTEGER, enginetemp INTEGER, ind1 BOOL, ind2 BOOL, ind3 BOOL, ind4 BOOL, ind5 BOOL, ind6 BOOL, ind7 BOOL, ind8 BOOL)''')
    cursor.execute('INSERT INTO vehicletelemetry (date,time,cyclecount,rpm,speed,odometer,oiltemp,airtemp,fuellevel,enginetemp,ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8) VALUES    (031514,013030,18960,3000,22,192768,210,72,98,210,0,0,0,0,0,0,0,0)')
    db.commit()
else:
    print('DB Table Exists')

#openfile to read

while(1):
    DataLine = ser.readline()
    Valid = bool(DataLine)
    if Valid == True:
            print(DataLine)
            #Read Database for last record date
            LastEntry = cursor.execute('SELECT * FROM vehicletelemetry ORDER BY id DESC LIMIT 1')
            for Records in LastEntry:
                    LastLogDate = Records[1]
                    LastLogTime = Records[2]
                    LastLogCycles = Records[3]
            DataLine1 = DataLine.strip()
            DataLine2 = DataLine1.split(',')
            print(DataLine2)

            #Check Packet for Correct Length
            if len(DataLine2) != 20:
                print (len(DataLine2))
                print ("Invalid Data Length")
            else:
                #Check Packet DataQualifiers to ensure proper package introduction and termination
                if DataLine2[0] != "7887" and DataLine2[18] != "0420":
                    print ("Invalid Data Qulifier")
                else:
                    #Remove Qualifiers So data can be stored
                    PiP = 1
                    DataLine3 = []
                    for Packet in DataLine2:
                        if PiP >= 1 and PiP <= 18:
                            DataLine3.append(DataLine2[PiP])
                            PiP = PiP + 1
                    #Compare Date Time and Cycle Count to Current Record
                    #print(DataLine3)
                    if int(DataLine2[1]) >= int(LastLogDate):    
                        if int(DataLine2[2]) >= int(LastLogTime):    
                            if int(DataLine2[3]) > int(LastLogCycles):
                                cursor.execute(InsertQuery,DataLine3)
                                db.commit()
                                print(Records,DataLine2[3],LastLogCycles,"Data Valid")

db.close()

当我运行代码时,它会检查数据的长度,但即使是正确的长度继续向下脚本,代码也会返回序列号readline(). 任何东西都不会写入数据库。你知道吗


Tags: pip代码executedbdatadateifinteger