2011-02-09 42 views
0

我加載一個名爲「rec.csv」我的MySQL數據庫文件在MySQL csv文件。我使用下面的代碼:問題批量加載使用Python

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)") 
[email protected]:~/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)") 

有人可以告訴我,我做錯了什麼? 感謝

回答

3

,讓您的負載查詢略有變化。

LOAD DATA LOCAL INFILE 

即,在查詢中添加LOCAL。

按照MySQL文檔: 如果指定了LOCAL,則該文件由客戶端主機上的客戶端程序讀取併發送到服務器。該文件可以作爲完整的路徑名來指定它的確切位置。如果作爲相對路徑名給出,則名稱將相對於客戶機程序啓動的目錄進行解釋。

檢查這個link以獲取更多信息。