我在文本文件中有数据需要上传到表格中。我的脚本在python 3中并使用mysql.connector(https://launchpad.net/myconnpy)连接到DB并执行命令。我已成功使用mysql.connector过去没有任何问题,但我在使用上传文件到表的命令时遇到问题。我的代码如下:
def TableUpload(con2):
cur = con2.cursor()##Connect to destination server with table
res_file = 'extend2'
cur.execute("TRUNCATE TABLE data.results")## Clear table before writing
cur.execute("LOAD DATA LOCAL INFILE './extend2' INTO TABLE data.results FIELDS TERMINATED BY ','")
代码清除表格,然后尝试将数据从文本文件上传到表格。它成功清除了表,但在填充表时生成了以下错误:
Traceback (most recent call last):
File "cl3.py", line 575, in <module>
TableUpload(con2)
File "cl3.py", line 547, in TableUpload
cur.execute("LOAD DATA LOCAL INFILE './extend2' INTO TABLE kakrana_data.mir_page_results FIELDS TERMINATED BY ','")
File "/usr/local/lib/python3.2/site-packages/mysql/connector/cursor.py", line 333, in execute
res = self.db().protocol.cmd_query(stmt)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/protocol.py", line 137, in deco
return func(*args, **kwargs)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/protocol.py", line 495, in cmd_query
return self.handle_cmd_result(self.conn.recv())
File "/usr/local/lib/python3.2/site-packages/mysql/connector/connection.py", line 180, in recv_plain
errors.raise_error(buf)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/errors.py", line 84, in raise_error
raise get_mysql_exception(errno,errmsg)
mysql.connector.errors.NotSupportedError: 1148: The used command is not allowed with this MySQL version
当我使用命令直接从终端上传文件时,它运行良好。只是命令不能从脚本中运行。该错误表明虽然它可以在终端上运行,但mysql版本不允许使用该命令。请告知我正在制作的错误或从本地文件上传数据到表中的替代方法。