简体   繁体   English

将 CSV 与 Python 导入到 MYSQL 时出错 - 类型错误:arguments 格式字符串不足

[英]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?任何人对此有一些提示我该如何解决?

Follow the image:按照图像: 在此处输入图像描述

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.

相关问题 TypeError:使用Python将CSV导入MySQL时,格式字符串的参数不足 - TypeError: not enough arguments for format string while importing CSV to MySQL using Python TypeError:格式字符串python mysql的参数不足 - TypeError: not enough arguments for format string python mysql MySQL/Python 中的“TypeError:没有足够的 arguments 用于格式字符串” - “TypeError: not enough arguments for format string” in MySQL/Python 我正在尝试使用 python 将 csv 文件导入 mysql。 我收到一个错误“TypeError:没有足够的 arguments 用于格式字符串” - I am trying to import a csv file to mysql using python. I am getting an error ' TypeError: not enough arguments for format string' TypeError:格式字符串Python3-MySQL的参数不足 - TypeError: not enough arguments for format string Python3-MySQL Python'TypeError:运行mysql查询时格式字符串'没有足够的参数 - Python 'TypeError: not enough arguments for format string' when running mysql query Python DataFrame 到 MYSQL:类型错误:没有足够的 ZDBC11CAA5BDA99F77E6FB4DABD882ED7 格式字符串 - Python DataFrame to MYSQL: TypeError: not enough arguments for format string MySQL TypeError:格式字符串的参数不足 - MySQL TypeError: not enough arguments for format string 如何在Python中修复'TypeError:格式字符串不足的参数'错误 - How to fix 'TypeError: not enough arguments for format string' error in Python Python 类型错误:arguments 格式字符串不足 - Python TypeError: not enough arguments for format string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM