如何在python中声明sql查询

时间:2014-06-09 20:52:13

标签: python mysql sql

我从我的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")

它在最后拾起我并导致它失败。我怎么摆脱这个?

1 个答案:

答案 0 :(得分:1)

错误消息显示,解析不是以空白结尾(显示"我"来自" INTO"接下来)。

您必须(单个)引用您的文件名,因此使用双引号组合SQL字符串以在文件名周围嵌入'

obsummaryfile = "'/home/abcd/projects/starling/daily_job_summary/" + starling_date + ".tsv'"

这有帮助吗?