mysql.connector.errors.ProgrammingError:1083(42000):字段分隔符参数不是预期的

时间:2016-12-07 19:50:54

标签: python mysql csv mysql-connector-python

当我使用Jupyter笔记本(Python)使用mysql.connector连接MySQL并将csv文件加载到数据库中时,在创建游标后,我尝试了:

    self.cursor.execute("LOAD DATA LOW_PRIORITY LOCAL INFILE 'deficiency.csv' \
    REPLACE INTO TABLE retailstore.deficiency CHARACTER SET utf8 \
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' \
    IGNORE 1 LINES")

我收到错误消息" mysql.connector.errors.ProgrammingError:1083(42000):字段分隔符参数不是预期的;查看手册"。

当我在MySQL中直接使用完全相同的sql查询加载csv文件时,它可以工作,而在Python中失败。

有人可以帮我确定问题所在吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试过print您尝试运行的SQL字符串,然后再将其传递给self.cursor.execute

如果你这样做,你会得到这个输出:

LOAD DATA LOW_PRIORITY LOCAL INFILE 'deficiency.csv'     REPLACE INTO TABLE retailstore.deficiency CHARACTER SET utf8     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ' ESCAPED BY ' LINES TERMINATED BY '
'     IGNORE 1 LINES

仔细看看这个,你会注意到它的一些事情:

  • OPTIONALLY ENCLOSED BYESCAPED BY仅遵循',而不是'"'正如您所期望的那样,
  • TERMINATED BY '之后有一个换行符。

要解决此问题,您需要在OPTIONALLY ENCLOSED BYESCAPED BY之后转义双引号字符,以及\r\n中的反斜杠:

self.cursor.execute("LOAD DATA LOW_PRIORITY LOCAL INFILE 'deficiency.csv' \
REPLACE INTO TABLE retailstore.deficiency CHARACTER SET utf8 \
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\r\\n' \
IGNORE 1 LINES")

或者,如果您使用三引号原始字符串,则不需要在\r\n中转义双引号字符和反斜杠,并且您也不需要在结尾处使用尾部反斜杠。每一行:

self.cursor.execute(r"""LOAD DATA LOW_PRIORITY LOCAL INFILE 'deficiency.csv'
    REPLACE INTO TABLE retailstore.deficiency CHARACTER SET utf8
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES""")

您可能想知道为什么您的原始代码生成了它所做的输出。原因是你实际上有三个字符串文字彼此相邻。其中之一是

"LOAD DATA LOW_PRIORITY LOCAL INFILE 'deficiency.csv' \
    REPLACE INTO TABLE retailstore.deficiency CHARACTER SET utf8 \
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"

下一个是

' ESCAPED BY '

(Python中的字符串文字可以被'"包围),最后一个是

"' LINES TERMINATED BY '\r\n' \
IGNORE 1 LINES"

如果将两个字符串文字放在一起,Python会将它们连接在一起:

>>> print("12"'34'"56")
123456