使用 Python 在 Mysql 中批量加载 csv 文件时出现问题
我正在将一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对负载查询稍作更改。
即,在查询中添加 LOCAL。
根据 MySQL 文档:
如果指定 LOCAL,则该文件由客户端主机上的客户端程序读取并发送到服务器。该文件可以作为完整路径名给出,以指定其确切位置。如果以相对路径名的形式给出,则该名称将相对于客户端程序启动的目录进行解释。
检查此链接了解更多信息。
Make a slight change in your load query.
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.