使用 Python 在 Mysql 中批量加载 csv 文件时出现问题

发布于 2024-10-16 22:30:38 字数 1710 浏览 0 评论 0原文

我正在将一个名为“rec.csv”的文件加载到我的 Mysql 数据库中。我使用下面的代码:

import MySQLdb,os

path='testData'
absPath = os.path.abspath(path)
print absPath

conn = MySQLdb.connect(host='localhost',
                          user='root',
                          passwd='',
                          db='iens')

db_cursor = conn.cursor()

query = "LOAD DATA INFILE '"+ absPath + "/rec.csv" +"' INTO TABLE iens.recensies FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' "

db_cursor.execute(query)
connection.commit()

由于某种原因,它找不到该文件!我可以看到该文件存在,并且在打印路径时它正在正确打印它。但最终它会产生这个错误:

File "Load_Data.py", line 18, in <module>
    db_cursor.execute(query)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.InternalError: (29, "File '/home/hossein/Documents/Parabots/DataBase/testData/rec.csv' not found (Errcode: 13)")
hossein@hossein-laptop:~/Documents/Parabots/DataBase$ python Load_Data.py
/home/hossein/Documents/Parabots/DataBase/testData
Traceback (most recent call last):
  File "Load_Data.py", line 18, in <module>
    db_cursor.execute(query)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.InternalError: (29, "File '/home/hossein/Documents/Parabots/DataBase/testData/rec.csv' not found (Errcode: 13)")

有人可以告诉我,我做错了什么吗? 谢谢

I am loading a file called 'rec.csv' to my Mysql DB. I use the code below:

import MySQLdb,os

path='testData'
absPath = os.path.abspath(path)
print absPath

conn = MySQLdb.connect(host='localhost',
                          user='root',
                          passwd='',
                          db='iens')

db_cursor = conn.cursor()

query = "LOAD DATA INFILE '"+ absPath + "/rec.csv" +"' INTO TABLE iens.recensies FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' "

db_cursor.execute(query)
connection.commit()

For some reason it can't find the file! I can see the file exists, and the when print the path it is printing it correctly. but in the end it generates this error:

File "Load_Data.py", line 18, in <module>
    db_cursor.execute(query)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.InternalError: (29, "File '/home/hossein/Documents/Parabots/DataBase/testData/rec.csv' not found (Errcode: 13)")
hossein@hossein-laptop:~/Documents/Parabots/DataBase$ python Load_Data.py
/home/hossein/Documents/Parabots/DataBase/testData
Traceback (most recent call last):
  File "Load_Data.py", line 18, in <module>
    db_cursor.execute(query)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.InternalError: (29, "File '/home/hossein/Documents/Parabots/DataBase/testData/rec.csv' not found (Errcode: 13)")

Can someone tell me, what I am doing wrong?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-10-23 22:30:38

对负载查询稍作更改。

LOAD DATA LOCAL INFILE

即,在查询中添加 LOCAL。

根据 MySQL 文档:
如果指定 LOCAL,则该文件由客户端主机上的客户端程序读取并发送到服务器。该文件可以作为完整路径名给出,以指定其确切位置。如果以相对路径名的形式给出,则该名称将相对于客户端程序启动的目录进行解释。

检查此链接了解更多信息。

Make a slight change in your load query.

LOAD DATA LOCAL INFILE

i.e., add LOCAL in the query.

As per MySQL docs:
If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full path name to specify its exact location. If given as a relative path name, the name is interpreted relative to the directory in which the client program was started.

Check this link for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文