Using pyserial, I am getting data from a sensor plugged into the USB port. I am trying to store that data, using MYSQLdb, into a database.
cur = db.cursor()
cur.execute("TRUNCATE TABLE randomdata;")
if ser.isOpen():
try:
i = 1
while 1:
num = str(1)
readserial = ser.readline()
print readserial
query="INSERT INTO randomdata(id,randomString)VALUES("+num+",'"+readserial+"');"
cur.execute(query)
db.commit()
i+=1
time.sleep(2)
if (i >= 50):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
It will store one value in the database before showing the error message: "error communicating...: (1062, "Duplicate entry '1' for key 'PRIMARY'")"
Any help would be greatly appreciated.