title: Continously insert arduino serial data in mysql using python
body: <p>i want to continuously insert arduino serial Data in MySQL using Python.
  I´ve already wrote a code. But only the first value is writing in mysql, by the second value, i get an error:"failed to get data from arduino". I hope somebody can help me and tell me what i´m doing wrong.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import serial
  import time
  import MySQLdb as mdb
  import sys
  
  
  #establish connection to MySQL. You'll have to change this for your database.
  dbConn = mdb.connect("localhost","root","messung","voc-messung") or die ("could not connect to database")
  #open a cursor to the database
  cursor = dbConn.cursor()
  
  device = 'COM7' #this will have to be changed to the serial port you are using
  
  
  try:
  print "Trying...",device
  arduino = serial.Serial(device, 9600)
  except:
  print "Failed to connect on",device
  try:
  while 2:
      time.sleep(2)
      data = arduino.readline()  #read the data from the arduino
      pieces = data.split("\t")  #split the data by the tab
      print pieces
          #Here we are going to insert the data into the Database
      try:
            #print 56  #verify the execution in the Try-loop
            cursor.execute ("INSERT INTO kammer_a (VOCWert) VALUES (%s)", (pieces[0]))
            print 5     #verify the execution in the Try-loop
  
            dbConn.commit() #commit the insert
            cursor.close()  #close the cursor
      except MySQLdb.IntegrityError:
           print "failed to insert data"
      finally:
          cursor.close()  #close just incase it failed
  except:
     print "Failed to get data from Arduino!"
  </code></pre>
  
  <p>I´m using Python2.7.
  The Arduino is connected to a Xbee module through Xbee shield  (coordinator in APi) and the other xbee is the router in AT mode</p>