加载CSV MySQLdb - Python

时间:2018-03-07 19:14:52

标签: python mysql mysql-python

我有一个CSV,我正在尝试加载到MySQL数据库中。我正在运行以下代码:

import MySQLdb

con = MySQLdb.connect(host="myhost",
                          user="me", 
                          passwd="mypw",
                          db="mydb")


cur = con.cursor()

sqlscript =r"""
DROP TABLE  IF EXISTS MyTable;

CREATE TABLE MyTable
            (Col1 VARCHAR(255), 
            Col2 VARCHAR(255), 
            CONSTRAINT PK_MyTable PRIMARY KEY (Col1));

LOAD DATA LOCAL INFILE 'C:\\Users\\me\\Documents\\Rec\\New Files\\mycsv.csv'
INTO TABLE MyTable
CHARACTER SET UTF8
FIELDS TERMINATED BY ','
ESCAPED BY '!'
ENCLOSED BY '"' 
OPTIONALLY ENCLOSED BY '\''
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;"""

cur.execute(sqlscript)
cur.close()

这样运行没有错误,但不会从我的CSV文件加载数据。它正确地删除表并使用脚本创建它。当我然后查询表时,它有零行。我错过了什么?

0 个答案:

没有答案