我正在从python脚本运行mysql插入查询,并且遇到以下错误-
jobsummaryfile = '/home/abcd/projects/starling/daily_job_summary/'+ starling_date +'.tsv'
conn = connect_db()
cursor = conn.cursor()
cursor.execute("LOAD DATA LOCAL INFILE " + jobsummaryfile + " INTO TABLE daily_job_summary FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' (@col1,@col2,@col3,@col4) set (jobname=@col1,queue=@col2,maphours=@col3,reducehours=@col4)")
conn.close()
我得到的错误是-
_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 '/home/abcd/projects/starling/daily_job_summary/2014_02_17.tsv I' at line 1")
它在最后获取I,这导致它失败。我该如何摆脱呢?
最佳答案
该错误消息向您显示,解析不以空白结尾(接下来向您显示“ INTO”中的“ I”)。
您必须(单引号)您的文件名,所以用双引号组成SQL字符串以在文件名周围嵌入'
obsummaryfile = "'/home/abcd/projects/starling/daily_job_summary/" + starling_date + ".tsv'"
这有帮助吗?
关于python - 如何在python中声明sql查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24128836/