I have a code, shown below that reads 4 lines of data over a serial connection. The data is assigned a variable and then an attempt is made to insert this into a local database. However, once the code has run, there is no new data in the database.
I have inserted print commands in to check that the data is definitely being received over terminal and it is, I have also successfully inserted data into the database via terminal, but that was static values such as 10.0, 10.0, 0, 10.
import MySQLdb
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 115200)
conn = MySQLdb.connect(host= "localhost", user= "JP", passwd= "password", db= "serialdb")
cursor = conn.cursor()
while 1:
print "waiting for data"
print ""
xs = ser.readline()
print xs
time.sleep(1)
ys = ser.readline()
print ys
time.sleep(1)
zs = ser.readline()
print zs
time.sleep(1)
vs = ser.readline()
print vs
time.sleep(1)
try:
x= float(xs)
except ValueError:
pass
try:
y= float(xs)
except ValueError:
pass
try:
z= float(xs)
except ValueError:
pass
v = int(vs)
print "inserting into database"
print ""
time.sleep(1)
sql = "INSERT INTO Arduino_Data(Temperature, Humidity, RPM, Distance) VALUES (%f, %f, %f, %d)" %(x, y, z, v)
cursor.execute(sql)
conn.commit
break
cursor.execute()to substitute parameters, rather than using string formatting.