简体   繁体   English

python中的Mysql命令产生:SyntaxError:扫描字符串文字时的EOL

[英]Mysql command in python yields: SyntaxError: EOL while scanning string literal

I get this error:我收到此错误:

SyntaxError: EOL while scanning string literal

while trying to push a CSV file of more than 20 million rows to MySQL server, using this python code:在尝试将超过 2000 万行的 CSV 文件推送到 MySQL 服务器时,使用以下 python 代码:

cur = connection.cursor()

query = "LOAD DATA LOCAL INFILE 'path to csv file' INTO TABLE table.name FIELDS OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n';"

cur.execute( query )

connection.commit()

When you enclose a string literal with a double quote in double quotes, you should either escape the double quote with a back slash:当您用双引号将字符串文字用双引号括起来时,您应该使用反斜杠转义双引号:

query = "LOAD DATA LOCAL INFILE 'path to csv file' INTO TABLE table.name FIELDS OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n';"

or enclose the string literal in triple quotes:或用三引号将字符串文字括起来:

query = '''LOAD DATA LOCAL INFILE 'path to csv file' INTO TABLE table.name FIELDS OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n';'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM