簡體   English   中英

MySQL/Python 負載數據本地文件錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM