python列表mysql_将python3列表中的值插入mysql

该博客演示了如何从用户输入获取Python列表,并尝试将这些值插入到MySQL数据库的特定行中。过程中遇到了一个编程错误,即参数数量与SQL语句中的占位符不匹配。解决方案是确保在执行插入操作时,提供的值的个数与表格列的数量一致,例如通过使用元组(tuple)来匹配占位符。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

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

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

解决方案

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.

Here the data is a list object.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值