import MySQLdb
# Open database connection
payload_list = ["10","20","30","40"]
db = MySQLdb.connect("localhost","","","iot")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
sql = "INSERT INTO iot_data (data_ip,data_mac,data_humidity,data_temp,data_time) VALUES('%s', '%s', '%s', '$s', NOW()))"
cursor.execute("INSERT INTO iot_data (data_ip,data_mac,data_humidity,data_temp,data_time) VALUES('%s', '%s', '%s', '$s', NOW()))",(payload_list))
# Fetch a single row using fetchone() method.
db.commit()
# disconnect from server
db.close()
ОшибкаЯ не могу INSERT INTO переменной в MYSQL с Python3
Traceback (most recent call last): File "db.py", line 13, in cursor.execute("INSERT INTO iot_data (data_ip,data_mac,data_humidity,data_temp,data_time) VALUES('%s', '%s', '%s', '$s', NOW()))",(payload_list[0],payload_list[1],payload_list[2],payload_list[3],)) File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 210, in execute query = query % args TypeError: not all arguments converted during string formatting
попробуйте удалить кавычки '% s' – jophab
Спасибо, очень полезный ответ –