0

我想要一个脚本,它遍历文件夹中的 csv 文件并将它们转储到 MySQL 数据库中。我能够将一个 csv 文件转储到其中。但是在将文件名传递给 SQL 脚本时遇到了麻烦。

这是我使用的代码

file_path="C:\csv-files"
files=os.listdir(file_path)
files.sort()
for n in files:

    cursor.execute(" LOAD DATA LOCAL INFILE '%s' INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' Lines terminated by '\n' IGNORE 1 LINES ",(n))

我收到以下错误

raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'file1.csv'' INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY' at line 1")

如果我直接使用文件名而不是传递它,它可以正常工作。

如果您可以在抛出的错误中看到,则 SQL 脚本中似乎存在错误。

这将是整个代码

import csv
import MySQLdb
import sys
import os
connection = MySQLdb.connect(host='localhost',
    user='root',
    passwd='password',
    db='some_db')


cursor = connection.cursor()

file_path="C:\csv-files"
files=os.listdir(file_path)
files.sort()
for n in files:
    print n



    cursor.execute(" LOAD DATA LOCAL INFILE %s INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' Lines terminated by '\n' IGNORE 1 LINES " %n)



connection.commit()
cursor.close()
4

1 回答 1

0

首先,将查询中的 '%s' 替换为 %s。MySQLdb 自动处理任何引用。

这是带有一些更正和更改的代码:

import MySQLdb
import os

CSV_DIR = "C:\csv-files"

connection = MySQLdb.connect(host='localhost',
                             user='root',
                             passwd='password',
                             db='some_db',
                             local_infile=1)

cursor = connection.cursor()

try:
    for filename in sorted(os.listdir(CSV_DIR)):
        cursor.execute("""LOAD DATA LOCAL INFILE %s
                          INTO TABLE new_table
                          FIELDS
                          TERMINATED BY ','
                          OPTIONALLY ENCLOSED BY '"'
                          ESCAPED BY '"'
                          LINES TERMINATED BY '\n'
                          IGNORE 1 LINES""",
                      (os.path.join(CSV_DIR, filename),))

    connection.commit()
finally:
    cursor.close()

注意:我在 MySQLdb.connect 中将 local_infile 参数设置为 1 并在元组中传递文件名以执行。

为我工作。

于 2013-02-13T16:37:44.007 回答