[英]MySQL/Python LOAD DATA LOCAL INFILE error
I am working with OSX 10.10.5 and MySQL 5.7.11 and trying to load txt data to mysql tables.我正在使用 OSX 10.10.5 和 MySQL 5.7.11 并尝试将 txt 数据加载到 mysql 表。 My code was working until I add load_data function.在我添加 load_data 函数之前,我的代码一直在工作。
import pymysql as mdb
import os
def connect(user, password):
return mdb.connect('localhost', user, password, local_infile=True)
def create_table(name):
cursor.execute("""drop table if exists %s """ % (name))
cursor.execute("""create table %s(try VARCHAR(4))""" %(name))
def load_data(file_name, table_name):
load_data="""load data local infile "%s" into table %s""" % (file_name, table_name)
cursor.execute(load_data)
con = connect('root','xxx')
cursor = con.cursor()
cursor.execute("use mydb")
table_names = ['dbtry','dbtry1','dbtry2']
file_names = os.listdir("/usr/local/mysql/bin/try")
for i in range(0,3):
create_table(table_names[i])
load_data(file_names[i],table_names[i])
con.close()
Then the code gave this error:然后代码给出了这个错误:
ERROR 1148: The used command is not allowed with this MySQL version
And it directed me to the:它引导我到:
http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html
Then I found "my-default.cnf" in "/usr/local/mysql/support-files" and copied it to "/etc".然后我在“/usr/local/mysql/support-files”中找到“my-default.cnf”并将其复制到“/etc”。 I added local-infile=1 under [mysqld].我在 [mysqld] 下添加了 local-infile=1。 When I asked MySQL:当我问 MySQL 时:
SHOW VARIABLES LIKE "local%";
it gave me:它给了我:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
Finally, when I added local_infile=True to return of the connect function, it raised a new error:最后,当我添加 local_infile=True 以返回连接函数时,它引发了一个新错误:
File "/Volumes/RADYO/dbtry.py", line 26, in <module>
load_data(file_names[i],table_names[i])
File "/Volumes/RADYO/dbtry.py", line 15, in load_data
cursor.execute(load_data)
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/cursors.py", line 158, in execute
result = self._query(query)
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/cursors.py", line 308, in _query
conn.query(q)
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/connections.py", line 820, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/connections.py", line 1002, in _read_query_result
result.read()
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/connections.py", line 1290, in read
self._read_load_local_packet(first_packet)
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/connections.py", line 1326, in _read_load_local_packet
sender.send_data()
File "/Users/elf/Desktop/elif/mysql-env/lib/python2.7/site-packages/pymysql/connections.py", line 1457, in send_data
raise err.OperationalError(1017, "Can't find file '{0}'".format(self.filename))
pymysql.err.OperationalError: (1017, "Can't find file 'try1.txt'")
But there is the file 'try1.txt' in the directory.但是目录中有文件'try1.txt'。 How can I solve the problem?我该如何解决问题?
The problem is likely in the pymysql client.问题很可能出在 pymysql 客户端。 It looks like you should be able to do the following:看起来您应该能够执行以下操作:
def connect(user, password):
return mdb.connect('localhost', user, password,local_infile=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.