使用python将csv文件导入Mysql数据库

时间:2013-03-04 21:43:22

标签: python mysql csv

这是我的代码:

#!/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语法中有错误; chekc与您的Mysql Server相对应的手册。

当我移除部件sqlLoadData += 'ENCLOSED BY '"' ESCAPED BY "\\" '时,一切都很完美。我使用最后一部分只是为了从值中删除引用。

我也尝试过:

  

cursor = mydb.cursor()

     

reader = csv.reader(open('Cumulative.csv','rb'))

     阅读器[1:]中的行的reader.next():       cursor.execute('INSERT INTO累积(C1,C2,C3,C4,C5,C6)值(%s,%s,%s,%s,%s,%s)',行)
   cursor.commit()

关闭与数据库的连接。

  

cursor.close()

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

任何人都可以帮我理解这个吗?

谢谢!

3 个答案:

答案 0 :(得分:3)

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

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

根据经验:当你进行调试时,你可以通过删除一行来修复你的代码,不要排除上面的

行>

编辑:修改了第二行的引号。我认为这是在“附上”声明中打破的。

答案 1 :(得分:2)

经过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()
希望它会帮助那里的人。

答案 2 :(得分:1)

经过数天和数小时的互联网搜索并遇到各种错误和警告,这完美无缺。我希望这能节省一些时间

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"

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