这是我的代码:#!/usr/bin/python
import MySQLdb
import csv
db = MySQLdb.connect(host="host", # The Host
user="username", # username
passwd="pwd", # password
db="databasename") # name of the data base
sqlLoadData = "LOAD DATA LOCAL INFILE "csv?_file_name.csv" INTO TABLE tablename "
sqlLoadData += "FIELDS TERMINATED BY "," LINES TERMINATED BY " ""
sqlLoadData += "IGNORE 1 LINES"
sqlLoadData += "ENCLOSED BY """ ESCAPED BY "\" "
try:
curs = db.cursor()
curs.execute(sqlLoadData)
resultSet = curs.fetchall()
except StandardError, e:
print e
db.rollback()
db.close()
我收到一条错误消息:您的SQL语法有错误;请检查与Mysql服务器相关的手册。
当我移除部件sqlLoadData += "ENCLOSED BY """ ESCAPED BY "\" "时,一切工作正常。我使用最后一部分只是为了从值中删除引号。
我也试过:cursor = mydb.cursor()
reader = csv.reader(open("Cumulative.csv", "rb"))
reader.next() for row in reader[1:]:
cursor.execute("INSERT INTO Cumulative (C1, C2, C3, C4, C5, C6) VALUES(%s, %s, %s, %s, %s, %s)", row)
cursor.commit()
关闭与数据库的连接。cursor.close()
我只想删除引号,这样整数字段将支持数据。所以带引号的“1”将被视为字符串而不是整数
有人能帮我理解这个吗?
谢谢!