简体   繁体   English

无法使用Python将CSV数据导入MySQL数据库

[英]Unable to import CSV data into MySQL database with Python

Next piece of Python source code I use to import data from CSV file into MySql. 下一段Python源代码我用来将数据从CSV文件导入MySql。 The installed versions in use are: MySql 5.7 and Python2.7 Furthermore, the table has 3 columns. 正在使用的已安装版本是:MySql 5.7和Python2.7此外,该表有3列。 One ID column (auto increment) and two text columns: Firstname and lastname. 一个ID列(自动增量)和两个文本列:名字和姓氏。

The matter seems to go wrong around the SQL statement. 围绕SQL语句似乎出了问题。 In particular the "%s" bit. 特别是“%s”位。

Literally, I have done everything with punctuates like: ",' and `. Eg 's', "s", ("s") etc. 从字面上看,我用一些标点做了所有事情:“,'和`。例如's',”s“,(”s“)等。

Also I have searched and used various source code snippets. 我也搜索并使用了各种源代码片段。 All of it failed. 所有这一切都失败了。 Even I doubted the quality of my CSV file. 即使我怀疑我的CSV文件的质量。 However, importing that via eg MySql workbench works fine. 但是,通过例如MySql工作台导入它可以正常工作。 Ploughing my way through I managed to get data in MySql when I add for instance some plain example values in the source code. 当我在源代码中添加一些简单的示例值时,我设法在MySql中获取数据。 So technically everything seems to be fine. 所以从技术上讲,一切似乎都很好。 It is just parsing which fails... 它只是解析哪个失败了......

Please help. 请帮忙。 Most probably I am overlooking something simple but it drives me utterly crazy... 最有可能的是我忽略了一些简单但却让我完全疯狂的事情......

import mysql.connector as mysql
import csv

db = mysql.connect(
    host = "xxxx",
    user = "xxxx",
    passwd = "xxxx",
    database = "abc"
)

cursor = db.cursor()

ifile  = open('/tmp/import.csv', "rb")
read = csv.reader(ifile)

for row in ifile:
    print row

    sql = "INSERT INTO Names(Firstname, Lastname) VALUES(%s, %s)"
    cursor.execute(sql, row)

    db.commit()

print(cursor.rowcount, "record inserted")

What I would expect to happen is dat the data in the CSV is parsed into MySQL. 我期望发生的是将CSV中的数据解析为MySQL的数据。 The error message I receive is: 我收到的错误消息是:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s)' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行使用'%s,%s,%s'附近的正确语法

Thanks for any hint, clue and/or solution!! 感谢任何提示,线索和/或解决方案!

Assuming you are reading the csv file correctly and you have same parameters required within the query try replacing this line 假设您正在正确读取csv文件并且查询中需要相同的参数,请尝试替换此行

cursor.execute(sql, row)

to

cursor.execute(sql, tuple(row))

Thanks for the answer. 感谢您的回答。 And apologies for this late reply. 并为此迟到的回复道歉。 The issue has become resolved in the mean time. 这个问题已经在同一时间得到解决。 It has to do with my CSV file. 它与我的CSV文件有关。 What exactly that is, I do not know yet. 究竟是什么,我还不知道。 However, I can continue with importing. 但是,我可以继续导入。

Thanks again and have a nice day! 再次感谢,祝你有愉快的一天!

Best regards, Detlev 最好的问候,Detlev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM