0

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
3
  • Why is your entire script inside quotes? Commented Oct 30, 2019 at 16:04
  • Apologies, that was me misunderstanding the formatting system, I tried to format it as '''code'''. Shadow edited it to fix it for me. Commented Oct 30, 2019 at 16:05
  • You should allow cursor.execute() to substitute parameters, rather than using string formatting. Commented Oct 30, 2019 at 16:07

1 Answer 1

2

Commit is a function, you are not calling it :)

conn.commit() 

That should do it

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, i had an inkling it was something daft.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.