我無法在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()
這不是語法錯誤,您輸入的文件路徑無效。 – aIKid
'navigation.get_dropbox_home'返回的方法是什麼? – aIKid