2013-10-07 32 views
1

我無法在python腳本中獲取MySQL查詢中的變量路徑。路徑變量或者用雙反斜線解決,或者根本不解決。如何在Python腳本中的MySQL查詢中使用可變路徑

這工作:

cursor.execute ("""load data local infile 'M:/Users/Jonathan/Dropbox/BCHS_3015/Spatial Data/Cartographic Data/USA/acs_data/Sequence_Number_and_Table_Number_Lookup.csv' 
       into table Sequence_Table_Lookup 
       fields terminated by ','enclosed by '"' 
       lines terminated by '\r\n' 
       ignore 1 lines 
       (File_ID,Table_ID,Sequence_Number,Line_Number, Subject_Area)"""); 

這下返回錯誤:

_mysql_exceptions.InternalError: (22, "File 'M:UsersJonathanDropbox\x08chs_3015spatial datacartographic datausaacs_dataSequence_Number_and_Table_Number_Lookup.txt' not found (Errcode: 22)") 

cursor.execute ("""load data local infile '%s' 
       into table Sequence_Table_Lookup 
       fields terminated by ','enclosed by '"' 
       lines terminated by '\r\n' 
       ignore 1 lines 
       (File_ID,Table_ID,Sequence_Number,Line_Number, Subject_Area)""" % filepath); 

刪除%左右產量的影響單引號

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right 
syntax to use near 'M:\\Users\\Jonathan\\Dropbox\\bchs_3015\\spatial data\\cartographic data\\usa\\acs_data\\' at line 1") 

我希望瞭解如何幫忙將可變路徑插入到MySQL查詢中。

我在一臺Windows機器上在Eclipse中使用PyDev。 Python 2.7和MySQLdb連接器。

的相關代碼的完整塊

conn = MySQLdb.connect (host = "localhost", 
          user = "user", 
          passwd = "pwd", 
          db = "gis_census_acs") 
#finds census directory 
dropbox = navigation.get_dropbox_home() 
acs_data = os.path.join(dropbox,'bchs_3015','spatial data','cartographic data','usa','acs_data'); 

for filepath in navigation.get_filepaths(acs_data): 
     filename = os.path.split(filepath)[1] 
     if filename == 'Sequence_Number_and_Table_Number_Lookup.txt': 
      print filepath; 
      tablename = filename.split('.')[0].replace(' ','_')[0:64] 
      cursor = conn.cursor() 
      cursor.execute ('create table if not exists ' + tablename + """(
        File_ID varchar(255), 
        Table_ID varchar(255), 
        Sequence_Number varchar(255), 
        Line_Number varchar(255), 
        Start_Position varchar(255), 
        Total_cells_in_Table varchar(255), 
        Total_Cells_in_Sequence varchar(255), 
        Table_title text, 
        Subject_Area text 
        )"""); 
      cursor.execute ("""load data local infile '%s' 
        into table Sequence_Table_Lookup 
        fields terminated by ','enclosed by '"' 
        lines terminated by '\r\n' 
        ignore 1 lines 
        (File_ID,Table_ID,Sequence_Number,Line_Number, Start_Position, 
        Total_cells_in_Table, Total_Cells_in_Sequence, Table_title, Subject_Area)""" % filepath); 
      print "Number of rows inserted: %d" % cursor.rowcount 
      cursor.close() 
     else: 
      print "not the file" 
conn.close() 
+0

這不是語法錯誤,您輸入的文件路徑無效。 – aIKid

+0

'navigation.get_dropbox_home'返回的方法是什麼? – aIKid

回答

0

該文件存在:

M:/Users/Jonathan/Dropbox/BCHS_3015/Spatial Data/Cartographic Data/USA/acs_data/Sequence_Number_and_Table_Number_Lookup.csv 

如你期望的那樣,這個奇怪的一個犯規:

M:UsersJonathanDropbox\x08chs_3015spatial datacartographic datausaacs_dataSequence_Number_and_Table_Number_Lookup.txt 

好像有什麼毛病你的文件路徑。嘗試檢查出來。

+0

謝謝@alKid。你是正確的路徑有問題。上面的第一條路徑是硬編碼的。第二個是變量如何解決。 'M:UsersJonathanDropbox'應該是'code'M:Users \ Jonathan \ Dropbox'的代碼'。它由方法'code'navigation.get_dropbox_home'code'返回。第二段'code'x08chs _... Lookup.txt'code'是文件的路徑。它由'code'navigation.get_filepath'code'返回。該方法使用'code'os.path.join'code'在目錄中創建路徑列表。循環中的'code'文件路徑'代碼'的打印語句有效。 SQL中的斜槓突破。 – jraviotta

+0

對不起,只是想出'代碼'反襯。 – jraviotta

+0

謝謝@alKid。你是正確的路徑是有問題的。上面的第一條路徑是硬編碼的。第二個是變量如何解決。 'M:UsersJonathanDropbox'應該是'M:Users \ Jonathan \ Dropbox \'。它由'navigation.get_dropbox_home'方法返回。第二段'x08chs _... Lookup.txt'是文件的路徑。它由'navigation.get_filepath'返回。該方法使用'os.path.join'在目錄中創建路徑列表。循環內的'filepath'的打印語句有效。 SQL中的斜槓突破。 – jraviotta

相關問題