1

我无法在 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);

删除 %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 ()
4

1 回答 1

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

好像你的文件路径有问题。试着检查一下。

于 2013-10-07T02:04:42.397 回答