如何在Python脚本中使用MySQL查询中的变量路径
在python脚本中获取MySQL查询的变量路径时遇到问题。path变量要么用双反斜杠解析,要么根本不解析 这项工作:如何在Python脚本中使用MySQL查询中的变量路径,python,mysql,sql,path,Python,Mysql,Sql,Path,在python脚本中获取MySQL查询的变量路径时遇到问题。path变量要么用双反斜杠解析,要么根本不解析 这项工作: 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
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);
删除%s左右的单引号将产生
_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 ()
此文件存在:
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
您的文件路径似乎有问题。请尝试签出。这不是语法错误,您输入的文件路径无效。navigation.get\u dropbox\u home方法返回什么?谢谢@alKid。你是正确的路径有问题。上面的第一条路径是硬编码的。第二个问题是变量的解析方式。”M:UsersJonathanDropbox'应该是'code'M:Users\Jonathan\Dropbox'code'。它是通过“code”导航方法返回的。get_dropbox_home“code”。第二段“code”x08chs_…Lookup.txt“code”是文件的路径。它由“代码”导航返回。获取文件路径“代码”。该方法使用'code'os.path.join'code'在目录中创建路径列表。循环中“code”文件路径“code”的打印语句有效。斜线在SQL中断开。谢谢@alKid。你是对的,路径有问题。上面的第一条路径是硬编码的。第二个问题是变量的解析方式。M:UsersJonathanDropbox应该是M:Users\Jonathan\Dropbox`。它由navigation.get\u dropbox\u home方法返回。第二段x08chs_…Lookup.txt`是文件的路径。它由navigation.get_filepath返回。该方法使用os.path.join在目录中创建路径列表。循环中filepath的print语句有效。斜杠在SQL中断开。