mardi 8 mars 2016

How to insert a multipart string into a sqlite table

I am stuck figuring out how to read a string from serial port, break it into it's parts and insert into a sqlite table. Below is the code I have so far, but I am missing the correct syntax to load the string correctly into sqlite. I can't change the incoming value.

#!/usr/bin/python
#--Arduino code calls this file when it detects flow during standard operation (i.e. when
#--not in calibration mode.  This code simply records the values from the sensor.
import serial
import sqlite3 as sqlite
#import sys

serial = serial.Serial("/dev/ttyUSB0", baudrate=115200)

code = ''

while True:
    data = serial.read()

    if data == '\r':
        con = sqlite.connect('/mnt/sda1/sensor_flow_moteino.db')
        cur = con.cursor()
        cur.execute('''INSERT INTO data_log (device,count) VALUES(?,?)'''(device,count))
        con.commit()
        con.close()
        print(code)
        code = ''

    else:
        code = code + data

Incoming data from the serial port is in the form: "some_string,123" where the first value is a string and the second value is an integer.

Aucun commentaire:

Enregistrer un commentaire