为什么我的代码永远不会写入数据库?

时间:2015-03-09 23:48:10

标签: python sqlite pyserial

我有这个代码,对象是读取一行序列,然后将其添加到数据库(如果它是有效的)。发送序列的代码以\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()

当我运行代码时,它会检查数据的长度,但即使是继续按下脚本的正确长度,代码也会返回到ser.readline()。什么都没有写入数据库。

0 个答案:

没有答案