How to use a variable path in MySQL query in a Python script -
i having trouble getting variable path mysql query in python script. path variable either resolved double backslashes or none @ all.
this works:
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' table sequence_table_lookup fields terminated ','enclosed '"' lines terminated '\r\n' ignore 1 lines (file_id,table_id,sequence_number,line_number, subject_area)""");
this following returns error:
_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' table sequence_table_lookup fields terminated ','enclosed '"' lines terminated '\r\n' ignore 1 lines (file_id,table_id,sequence_number,line_number, subject_area)""" % filepath);
removing single quotes around %s yields
_mysql_exceptions.programmingerror: (1064, "you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'm:\\users\\jonathan\\dropbox\\bchs_3015\\spatial data\\cartographic data\\usa\\acs_data\\' @ line 1")
i appreciate in understanding how insert variable path mysql query.
i using pydev in eclipse on windows machine. python 2.7 , mysqldb connector.
the full block of relevant code
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'); 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' table sequence_table_lookup fields terminated ','enclosed '"' lines terminated '\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 file" conn.close ()
this file exists:
m:/users/jonathan/dropbox/bchs_3015/spatial data/cartographic data/usa/acs_data/sequence_number_and_table_number_lookup.csv
as expect, strange 1 doesnt:
m:usersjonathandropbox\x08chs_3015spatial datacartographic datausaacs_datasequence_number_and_table_number_lookup.txt
seems there's wrong filepath. try checking out.
Comments
Post a Comment