[英]Error importing CSV With Python to MYSQL - TypeError: not enough arguments for format string
I'm facing a problem trying to insert a CSV file with 800 records in the database.我在尝试在数据库中插入包含 800 条记录的 CSV 文件时遇到问题。 This gives me the following error: MySQLdb.ProgrammingError: not enough arguments for format string.这给了我以下错误:MySQLdb.ProgrammingError:arguments 格式字符串不足。 I already checked and the exchanges and variables are correct, could you help me what would this problem be?我已经检查过,交换和变量是正确的,你能帮我看看这个问题是什么吗? Here is the code:这是代码:
import MySQLdb
import csv
conn = MySQLdb.connect(host="127.0.0.1", user="root", password="", database="csm")
cursor = conn.cursor()
csv_data = csv.reader(open('teste2.csv'))
header = next(csv_data)
for row in csv_data:
print(row)
cursor.execute(
"INSERT INTO estoque1 (release, official, order, date, product, client, sales, sales, quant) VALUES (%s, %s, %s, %s, %s ,%s ,%s ,%s ,%s)", row)
conn.commit()
cursor.close()
I'm facing this error but idk how to solve this.我正面临这个错误,但我不知道如何解决这个问题。 Anyone have some tips about this how can i solve?任何人对此有一些提示我该如何解决?
Because you're passing the array as an argument while the execute()
function expects all the elements of the array as a single argument.因为您将数组作为参数传递,而execute()
function 期望数组的所有元素作为单个参数。
You should be able to pass the array like this:您应该能够像这样传递数组:
cursor.execute(
"INSERT INTO estoque1 (release, official, order, date, product, client, sales, sales, quant) VALUES (%s, %s, %s, %s, %s ,%s ,%s ,%s ,%s)",
*row
)
Note the asterisk.注意星号。
The Problem is that you only pass one parameter (the row list) to the 9 placeholders in your string.问题是您只将一个参数(行列表)传递给字符串中的 9 个占位符。 To fix that you need either convert the list to a tuple, use the "*" operator to unpack the list or pass all the values of the list individually.要解决此问题,您需要将列表转换为元组,使用“*”运算符解压缩列表或单独传递列表的所有值。
Use the * operator to unpack:使用 * 运算符解包:
row = [1, 2, 3, 4]
"%s %s %s %s" % (*row,)
Use tuple:使用元组:
row = [1, 2, 3, 4]
"%s %s %s %s" % tuple(row)
Or you list all parameters extra:或者您额外列出所有参数:
row = [1, 2, 3, 4]
"%s %s %s %s" % (row[0], row[1], row[2], row[3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.