2

这是我的代码:

#!/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 "\n"'     
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 "\\" '一切都很完美。我使用最后一部分只是为了从值中删除引号。

我也试过:

游标 = 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)', 行)
cursor.commit()

关闭与数据库的连接。

光标.close()

我只想删除引号,以便整数字段支持数据。所以带引号的“1”将被视为字符串而不是整数

任何人都可以帮助我理解这一点吗?

谢谢!

4

3 回答 3

4

看起来您忘记用空格或换行符终止前一行。当解析器试图理解LINESENCLOSED哪个显然不是关键字时,这会导致语法错误。

 sqlLoadData += 'IGNORE 1 LINES \n'
 sqlLoadData += ''ENCLOSED BY '"' ESCAPED BY "\" ''

作为一个经验法则:当你在调试时,你可以通过删除一行来修复你的代码,不要排除上面的行

编辑:修改了第二行的引号。我认为它打破了“封闭式”声明。

于 2013-03-04T21:45:11.973 回答
2

经过两天的研究,我找到了答案:

!/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

 cursor = connection.cursor() 
 Query = """ LOAD DATA LOCAL INFILE 'usrl to csv file' INTO TABLE
 table_nameFIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED
 BY '"' Lines terminated by '\n' IGNORE 1 LINES """

cursor.execute(Query)   
connection.commit()   
cursor.close()

希望它会帮助那里的人。

于 2013-03-05T18:04:53.720 回答
2

经过数天和数小时的互联网搜索并遇到各种错误和警告,这非常有效。我希望这可以节省一些时间

import MySQLdb
import os
import string

db = MySQLdb.connect (host="host",
    user="user",
    passwd="pwd",
    db="database_name",
    local_infile = 1) #Grants permission to write to db from an input file. Without this you get sql Error: (1148, 'The used command is not allowed with this MySQL version')

print "\nConnection to DB established\n"

#The statement 'IGNORE 1 LINES' below makes the Python script ignore first line on csv file
#You can execute the sql below on the mysql bash to test if it works
sqlLoadData = """load data local infile 'file.csv' into table table_name FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;"""

try:
    curs = db.cursor()   
    curs.execute(sqlLoadData)
    db.commit()   
    print "SQL execution complete"   
    resultSet = curs.fetchall()   
except StandardError, e:    
    print "Error incurred: ", e    
    db.rollback()
    db.close()

print "Data loading complete.\n"

谢谢,我希望这会有所帮助:)

于 2018-02-25T01:40:26.843 回答