简体   繁体   English

使用python 2.7将csv文件加载到mysql中

[英]load csv file into mysql using python 2.7

I am trying to insert csv file into mysql using python. 我正在尝试使用python将csv文件插入mysql。 I am just new programmer for mysql, so I am not sure what is the problem. 我只是mysql的新程序员,所以我不确定是什么问题。 Here is my code: 这是我的代码:

file = csv.reader(file('20184329:2143.csv'))
for row in file:
    sql_insert_table = ("""INSERT INTO STAGING(ADRESSE_1600 ,
                        ADRESSE_1601, ADRESSE_1602, ADRESSE_1603)
                        VALUES ('%s', '%d', '%d', '%s')""", row)
    cursor.execute(sql_insert_table)

And here is the problem: 这是问题所在:

Traceback (most recent call last):
  File "python_loaddata.py", line 22, in <module>
    cursor.execute(sql_insert_table)
  File "/usr/lib/python2.7/site-packages/MySQLdb/cursors.py", line 161, in execute
    self.errorhandler(self, TypeError, m)
  File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
TypeError: must be string or read-only buffer, not tuple

According to the MySQL execute method docs , the first argument should be the SQL string. 根据MySQL execute方法docs ,第一个参数应该是SQL字符串。

You are passing a tuple, with the query and the tuple. 您正在传递带有查询和元组的元组。

So just change to: 因此,只需更改为:

file = csv.reader(file('20184329:2143.csv'))
sql_insert_table = """INSERT INTO STAGING(ADRESSE_1600 ,
                      ADRESSE_1601, ADRESSE_1602, ADRESSE_1603)
                      VALUES ('%s', '%d', '%d', '%s')"""
for row in file:
    cursor.execute(sql_insert_table, params=row)

Note that you can just pass the row as is because the csv.reader returns a list of strings. 请注意,您可以按csv.reader传递行,因为csv.reader返回字符串列表。

cursor.execute() expects a sql statement (as string) and an optional sequence of values, so it should have been either: cursor.execute()需要一个sql语句(作为字符串)和一个可选的值序列,因此它应该是:

# this will build a (statement, (values,....)) tuple
args = "INSERT INTO STAGING(ADRESSE_1600 ,
                    ADRESSE_1601, ADRESSE_1602, ADRESSE_1603)
                    VALUES ('%s', '%d', '%d', '%s')", (row[i],i)

# so you need positional arguments unpacking:
cursor.execute(*args)

or you can use: 或者您可以使用:

sql = "INSERT INTO STAGING(ADRESSE_1600 ,
                    ADRESSE_1601, ADRESSE_1602, ADRESSE_1603)
                    VALUES ('%s', '%d', '%d', '%s')"
cursor.execute(sql, (row[i],i))

You've got two problems here, and are close to a common security hole. 您在这里遇到两个问题,并且接近一个常见的安全漏洞。

It looks like you're trying to use % -style substitution to create your insert statement. 似乎您正在尝试使用% -style替换来创建您的插入语句。 I say that because you've quoted your parameters in the SQL string. 我之所以这样说是因为您已经在SQL字符串中引用了参数。 That's a really, really bad idea : if somebody enters their address as, say, ');DROP TABLE STAGING;-- then you'll end up executing their SQL. 这是一个非常非常糟糕的主意 :如果有人输入他们的地址,例如');DROP TABLE STAGING;--那么您最终将执行他们的SQL。 Instead, use SQL-style placeholders. 而是使用SQL样式的占位符。 Those show up as if they were just literals, like VALUES (%s) , not VALUES ('%s') . 它们似乎只是文字,如VALUES (%s)而不是VALUES ('%s')

Even though the parameters look like they're just going to be %-substituted, as if you used Python's % operator, they're not. 即使参数看起来像只是要被%替换,就像您使用Python的%运算符一样,它们也不是。 The actual values will be used in their stead. 实际值将代替它们使用。 So, don't quote them. 所以,不要引用它们。

The reason for your error, though, is that the statement and the parameters to be used are two separate arguments to cursor.execute . 但是,导致错误的原因是该语句和要使用的参数是cursor.execute两个独立参数。 You're currently passing them in as a single argument that's a 2-long tuple. 您当前正在将它们作为单个参数传递,该参数为2长元组。

Here's a fixed version: 这是固定版本:

sql_insert_table = """INSERT INTO STAGING(ADRESSE_1600 ,
                   ADRESSE_1601, ADRESSE_1602, ADRESSE_1603)
                   VALUES (%s, %s, %s, %s)"""
file = csv.reader(file('20184329:2143.csv'))
for row in file:
    cursor.execute(sql_insert_table, row)

她的女儿名为“帮助我”,被困在驾驶执照工厂。

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

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