python pypyodbc writing to dBase not working

201 views Asked by At

Have a terrible time trying to get this working. Spent way too many hours doing searches only to come up with the same examples which are not helping.

Made myself a GUI that starts a Thread to read the serial ports and parse some GPS data into NMEA sentences.

I take the data and want to write it to a database instead of a text file just to make it cleaner. However, when I write to the DB it throws an error saying that:

'type' object has no attribute '__getitem__'

As I have zero experience dealing with databases and python I'm at an impass as to what is wrong.

I know its the cur.execute INSERT line tossing the exception, I just don't know how to fix it.

Anyone care to walk me through it? I'm guessing it might be syntax? I'm using the pynmea2 module for GPS parsing and the pypyodbc module to make the MDB file. If I write hard-coded-data it works fine. It's when I use variables that it craps out.

                        try:
                            print ("Attempt to Execute...")
                            Ndx=ID;Tod=str(gps_msg.timestamp);Lat=str(gps_msg.latitude);Lon=str(gps_msg.longitude);TmpLat='0';TmpLon='0';Alt=str(gps_msg.altitude);Flags='0';SystemID=str(gps_msg.ref_station_id);NumSat=str(gps_msg.num_sats);dop='0';Ch1_RSSI=str(rssi_dB)
                            cur.execute("INSERT INTO LogFile(Ndx,Tod,Lat,Lon,TmpLat,TmpLon,Alt,Flags,SystemID,NumSat,dop,Ch1_RSSI) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",([Ndx],[Tod],[Lat],[Lon],[TmpLat],[TmpLon],[Alt],[Flags],[SystemID],[NumSat],[dop],[Ch1_RSSI]))
                            print ("Attempting to commit...")                            
                            conn.commit()
                            print ("Commit succeeded..")


                           ID = ID + 1


                        except Exception as e:
                           errorLog.writelines('File Write Failure: {0}'.format(e)+"\n")
1

There are 1 answers

0
Gord Thompson On BEST ANSWER

You are trying to pass the parameter values as a tuple whose elements are single-item lists. That won't work. The parameters must be passed as a list (or tuple) of individual values or objects.

So, instead of passing

([Ndx],[Tod],[Lat], ... )

you should be passing

[Ndx,Tod,Lat, ... ]

as the second argument to the .execute() method.