I am trying to insert data received through serial into a mysql table using python 3, however, when I try to input my insert statement I get a "TypeError: not enough arguments for format string". Any help would be greatly appreciated!
I tried using ", (new_data)"
instead of "% (new_data)"
and got SQL syntax error. Also tried ".format(new_data)"
which did not produce an error, but resulted in empty string fields or 0.00 float fields.
raw_data=ser.readline() #Fetch a line from the serial feed
new_data=raw_data.decode('ascii').rstrip() #Remove extraneous control characters
new_data=(new_data, ) #Convert to a tuple
#Result
#("'2019-5-21 7:55:5',0,3.1,25.6,315,2.2,316,11.0,248,56.6,56.9,0.00,0.73,100375.50,4.37,0.83",)
#Now insert into the database
sql="INSERT INTO reports (rec_time, wnddir, wndspd, wndgust, wndgustdir, wndspdavg2m, wnddiravg2m, wndgustavg10M, wndgustdiravg10M, humidity, tempf, rain, raindly, pressure, battery, light) VALUES ('%s', '%d', '%f', '%f', '%d', '%f', '%d', '%f', '%d', '%f', '%f', '%f', '%f', '%f', '%f', '%f')" % (new_data)
cursor.execute(sql)
I am expecting a record to be written to the database, however, I get a "TypeError: not enough arguments for format string". There are 16 fields, 16 placeholders and 16 pieces of data. What am I doing incorrectly?