0

我在文本文件中有数据,需要将其上传到表格中。我在 python 3 中的脚本并使用 mysql.connector (https://launchpad.net/myconnpy) 连接到数据库并执行命令。我过去已经能够成功使用 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 版本,尽管它可以从终端运行。请提出我犯了什么错误或替代方法来实现数据从本地文件上传到表。

4

0 回答 0