[英]Python2.7 MySQL Connector Error on LOAD DATA LOCAL INFILE
[英]MySQL/Python LOAD DATA LOCAL INFILE error
我正在使用 OSX 10.10.5 和 MySQL 5.7.11 并尝试将 txt 数据加载到 mysql 表。 在我添加 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()
然后代码给出了这个错误:
ERROR 1148: The used command is not allowed with this MySQL version
它引导我到:
http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html
然后我在“/usr/local/mysql/support-files”中找到“my-default.cnf”并将其复制到“/etc”。 我在 [mysqld] 下添加了 local-infile=1。 当我问 MySQL 时:
SHOW VARIABLES LIKE "local%";
它给了我:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
最后,当我添加 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'")
但是目录中有文件'try1.txt'。 我该如何解决问题?
问题很可能出在 pymysql 客户端。 看起来您应该能够执行以下操作:
def connect(user, password):
return mdb.connect('localhost', user, password,local_infile=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.