Python and SQL: Getting rows from csv results in ERROR: "There are more columns in the INSERT statement than values specified in the VALUES clause."

Python and SQL: Getting rows from csv results in ERROR: "There are more columns in the INSERT statement than values specified in the VALUES clause."

我有一个包含多条记录的 csv 文件,我正试图通过 Python 脚本将其导入 SQL table。我的 csv 文件(现在减少为)只有一行 1。这是我正在尝试做的事情(在成功连接到数据库等之后...):

def add_records():
    with open('C:/testing/myCSV.csv') as csvFile:

        for row in csvFile:
            cursor.execute(
                "INSERT INTO MY_Table (thing1, thing2, thing3, thing4, thing5)"
                "VALUES (?)", row
            )

无论我如何格式化 csv 中的数据(现在都是 1),我都会收到错误消息:

There are more columns in the INSERT statement than values specified in the VALUES clause

您需要在 values 子句中为要插入的每一列指定一个值(或 ? 占位符):

cursor.execute(
    "INSERT INTO MY_Table (thing1, thing2, thing3, thing4, thing5)"
    "VALUES (?, ?, ?, ?, ?)", row
)

编辑:
row 只是从 CSV 文件中读取的简单行。您可能应该使用 csv reader 将其分解为各个组件:

with open('C:/testing/myCSV.csv') as csvFile:
    csvReader = csv.reader(csvFile)
    for row in csvReader:
        cursor.execute(
            "INSERT INTO MY_Table (thing1, thing2, thing3, thing4, thing5)"
            "VALUES (?, ?, ?, ?, ?)", row
        )
with open('C:/testing/myCSV.csv') as csvFile:
    factories = csv.reader(csvFile, delimiter=' ')
    for row in factories:
        cursor.execute(
            "INSERT INTO MY_Table (thing1, thing2, thing3, thing4, thing5)"
            "VALUES (?, ?, ?, ?, ?)", (''.join(row[0])), (''.join(row[1])), (''.join(row[2])), (''.join(row[3])), (''.join(row[4]))
        )