m = input('Num1:')
m = int(m)
x1 = (m-1)
#
m = input('Num2:')
m = int(m)
x2 = (m-1)
#
z = [0,0,0,0,0,0,0,0,0]
z[(x1)] = 1
z[(x2)] = 1
##########################################
import mysql.connector
list = z
print(list)

mydb = mysql.connector.connect(host="localhost", user="root", passwd="onsdag", db="eno")
conn = mydb.cursor()

params = ['?' for item in list]
sql = 'INSERT INTO bola (n01, n02, n03, n04, n05, n06, n07, n08, n09) VALUES (%s);' % ','.join(params)
conn.execute(sql, list)

mydb.commit()

print(conn.rowcount, 'record inserted')


print(conn.rowcount, 'record inserted')


Num1:5
Num2:8
[0, 0, 0, 0, 1, 0, 0, 1, 0]
Traceback (most recent call last):
  File "inputxx.py", line 26, in <module>
    conn.execute(sql, list)
  File "/home/amb/.local/lib/python3.6/site-packages/mysql/connector/cursor.py", line 543, in execute
    "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

最佳答案

问题出在您的MySQL插入脚本中。您可以如下所示进行更新。

conn.execute("INSERT INTO bola(n01, n02, n03, n04, n05, n06, n07, n08, n09) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)",tuple(data))


您的值计数应始终与列数匹配。

这里的datalist对象。

关于python - 将python3列表中的值插入mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59552459/

10-11 16:35
查看更多