LOAD DATA LOCAL INFILE不将文件加载到数据库

时间:2016-04-20 13:08:02

标签: mysql python-2.7 csv

我有一个.csv文件,我想使用LOAD DATA INFILE导入数据库。 csv文件包含超过3000行和5列。运行下面的代码后,我收到一条错误消息:

  

(22,“未找到文件'MyScripts \ name.csv'(错误代码:22)”)

conn_mysql与name.csv位于同一目录中。如有任何帮助,将不胜感激。

import MySQLdb
login_info = open("MyScripts\\conn_mysql.txt").read()
login_info = login_info.split()

conn = MySQLdb.connect(host=login_info[0],port=int(login_info[1]),user=login_info[2],passwd=login_info[3],db="test")
print("Connected")
cursor = conn.cursor()
sql = """LOAD DATA LOCAL INFILE 'MyScripts\\name.csv' \
  INTO TABLE names \
  FIELDS TERMINATED BY ',' \
  OPTIONALLY ENCLOSED BY '"'  \
  LINES TERMINATED BY '\r\n' \
  IGNORE 1 LINES;;"""
try:
   cursor.execute(sql)
   conn.commit()
except Exception as e:
      print("%s" % e)
      conn.rollback()
conn.close()

0 个答案:

没有答案