I want to read the data of a sensor and transfer it to MySQL local host with
mysql.connector
. The problem is that it reads the data as a list containing 500 data (that is, it reads 500 to 500, not one by one). This is my code:
master_data = master_task.read(number_of_samples_per_channel=500)
sql = "INSERT INTO vib_data2 (data) VALUES (%s)"
val = list(master_data, )
mycursor.executemany(sql, val)
mydb.commit()
I got this error: Could not process parameters: float(-0.15846696725827258), it must be of type list, tuple or dict
I can fix the problem with this code:
for i in master_data:
sql = "INSERT INTO vib_data2 (data) VALUES (%s)"
val = (str(I),)
mycursor.execute(sql, val)
mydb.commit()
But the code execution time is very long (more than one second) and thus some of the data is lost.
Thank you for helping to correct the code
Please show the full traceback (not just the error). It can provide useful information about the problem.
What does master_data contain? What does print(repr(master_data)) and print(type(master_data)) show?
val = list(master_data, ) — Why is there the comma? It should not change the result but it is very confusing.
val = (str(I),) — Where does I come from? Did you replace i by I by mistake?
mydb.commit() — Do you need to commit in every iteration of the loop? Why not just after the loop?
Anyway - guessing what could be the problem