Using a for loop in python to generate mySQL insert statements
Question: I am working on a python script to take an incoming String of data from
several arduinos to read, split and insert that data into a database.

The problem is the sensor data varies in the number of values depending on
what kind of sensor is used. I cannot figure out the proper way to loop
through the separated pieces and insert them properly.

A '1' specifies 10HS sensor and need 1 space for the incoming value  
The String comes in like this for 10HS sensors:  
"Cucumber2015,Arduino01,1,20150918124200,25.3,75.5,**_1_ ,12 .....**  
  
A '2' specifies 10HS sensor and need 1 space for the incoming value  
"Cucumber2015,Arduino01,1,20150918124200,25.3,75.5,**_2_ ,12,24,23 ......**

The for loop should repeat until all the sensor values have an insert
statement for their respective tables.

I have tried the code shown below and keep getting errors.

How can I accomplish this? Besides the syntax problem, am I going about this
correctly?

**My current error**

    
    
      File "serialToDbV3.py", line 50
        index =index+1
            ^
    SyntaxError: invalid syntax
    

**Python code**

    
    
    #!/usr/bin/python
    
    import serial 
    import MySQLdb
    
    #establish connection to MySQL. You'll have to change this for your database.
    dbConn =     MySQLdb.connect("localhost","python_user","password","IrrigationDB") or die ("could not connect to database")
    #open a cursor to the database
    cursor = dbConn.cursor()
    
    device = '/dev/ttyUSB0' #this will have to be changed to the serial port you are using
    baudrate = 9600
    
    def getSerialData():
    try:
        print "Trying...",device
        arduino = serial.Serial(device, baudrate) 
    except: 
        print "Failed to connect on",device    
    
    try:
        print "Trying to get data"
        next(arduino)
        data = arduino.readline()  #read the data from the Arduino
        pieces = data.split(",")  #split the data by the tab
        print "Data: %s" % data
        print "Piece 0: ProjectID %s" % pieces[0]
        print "Piece 1: ArduinoID %s" % pieces[1]
        print "Piece 2: Plot#     %s" % pieces[2]
        print "Piece 3: SQLTime   %s" % pieces[3]
        print "Piece 4: AirTemp   %s" % pieces[4]
        print "Piece 5: Humidity  %s" % pieces[5]
        print "Piece 6: SensType  %s" % pieces[6]
        print "Piece 7: SensData  %s" % pieces[7]
        print "Piece 8:           %s" % pieces[8]
        print "Piece 9:          %s" % pieces[9]
        print "Piece 10:          %s" % pieces[10]
    #Here we are going to insert the data into the Database
        try:
            print "Trying insertion..."
            cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Project`(`idProject`)VALUES (%s)", (pieces[0]))     
            cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Arduino`(`idArduino`,`FK_ProjectID`)VALUES (%s,%s)", (pieces[1],pieces[0]))     
            cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Plot`(`idPlot`,`FK_ArduinoID`)VALUES (%s,%s)", (pieces[2],pieces[1]))       
            cursor.execute("INSERT INTO `IrrigationDB`.`Reading`(`DateAndTime`,`airTempC`,`relativeHumidity`,`FK_PlotID`)VALUES (%s,%s,%s,%s)", (pieces[3],pieces[4],pieces[5],pieces[2]))       
    
            startingPosition = 6
            for index in xrange(startingPosition, len(pieces), step):
                if pieces[6] == 1 :
                    cursor.execute("INSERT INTO `IrrigationDB`.`10HS_Sensor`(`id10HS_Sensor`,`DielectricPermittivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1])
                    index =index+1
                if pieces[6] == 2 :
                    cursor.execute("INSERT INTO `IrrigationDB`.`GS3_Sensor`(`idGS3_Sensor`,`DielectricPermittivity`,`soilTempC`,`electricalConductivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1],pieces[i+2],pieces[i+3]))
                    index=index+3
        except MySQLdb.IntegrityError:
            print "failed to insert data"         
    except:
        print "Failed to get data from Arduino!" 
    
    val = 0
    while val == 0 :
        getSerialData()
    


Answer: 
    cursor.execute("INSERT INTO `IrrigationDB`.`10HS_Sensor`(`id10HS_Sensor`,`DielectricPermittivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1])
    

You're missing a parentheses at the end, it should be:

    
    
    cursor.execute("INSERT INTO `IrrigationDB`.`10HS_Sensor`(`id10HS_Sensor`,`DielectricPermittivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1]))
    

