加载数据本地infile在Ubuntu 14.4 LTS中不起作用

时间:2018-04-18 07:59:08

标签: python mysql load

path = os.path.join(os.path.expanduser('~'), 'pythonscript', 'ftp', fname) print(path) print ("-------------------load starts for "+tbname+"---------------------------------------------\n") query = """load data local infile"""+' '+path+' '+"""into table"""+' '+tbname+' '+"""fields terminated by"""+' '+"'|'"+' '+"""lines terminated by"""+' '+"'"+lt+"'"+' '+"""ignore 1 lines;""" cur.execute(query) 它在我的本地Windows系统中工作。

在上面的代码“ fname ”中:文件名和“路径”:文件路径

使用 mysql.connector.connect 进行数据库连接后,我运行load语句得到以下错误,任何帮助将不胜感激。提前谢谢。

错误:Traceback (most recent call last): File "load_to_mysql.py", line 73, in <module> cur.execute("""load data local infile"""+' '+path+' '+"""into table"""+' '+tbname+' '+"""fields terminated by"""+' '+"'|'"+' '+"""lines terminated by"""+' '+"'"+lt+"'"+' '+"""ignore 1 lines"""+";") File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 551, in execute self._handle_result(self._connection.cmd_query(stmt)) File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 490, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 395, in _handle_result raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): 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/ubuntu/pythonscript/ftp/INV_COST_FEED_GARBJ_20180417_00.txt into table ftp' at line 1 ubuntu@ip-X-X-X-XXX:~/pythonscript/ftp$ nano load_to_mysql.py

1 个答案:

答案 0 :(得分:0)

看起来你错过了文件路径

周围的引号

语法在'/home/ubuntu/pythonscript/ftp/INV_COST_FEED_GARBJ_20180417_00.txt附近使用到第1行的表ftp'

See docs

  

文件名必须以文字字符串形式给出。在Windows上,指定   路径名中的反斜杠为正斜杠或双反斜杠。   character_set_filesystem系统变量控制   解释文件名。