Skip to content
Advertisement

Python SQL insert statement not working

I am trying to insert Arduino data into a database through Python, however, it will not do it. Basically I am assigning data that I read in from the serial port assigned to my Arduino and storing the first value of it in the variable arduinoData. in my insert statement I am trying to use a string literal to put the arduinoData into the table. Here is the code:

JavaScript

If I put the %s in single quotes like '%s' it just prints that instead of my arduinoData. Can anyone see what is wrong here, thanks.

Advertisement

Answer

Simply pass a tuple (arduinoData,) which means a comma within parentheses for a single value or list [arduinoData] and not a single value (arduinoData) in your parameterization:

JavaScript

However if arduinoData is a list of multiple values, then use executemany, still passing a list. Also, escape value which is a MySQL reserved word:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement