将python3列表中的值插入mysql

编程入门 行业动态 更新时间:2024-10-26 18:19:37
本文介绍了将python3列表中的值插入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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) mydbmit() 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插入脚本中.您可以如下所示进行更新.

The problem is in your MySQL Insert script. You can update this as shown below.

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))

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

Your values count should always match the number of columns.

此处data是list对象.

更多推荐

将python3列表中的值插入mysql

本文发布于:2023-06-08 14:29:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表中   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!