2013-10-02 43 views
3

我想動態加載人口普查數據到MySQL數據庫(從.csv文件)使用Python和MySQL連接器。Python2.7 MySQL連接器錯誤LOAD數據本地INFILE

我想不通爲什麼我收到的錯誤:

Traceback (most recent call last): 
    File "miner.py", line 125, in <module> 
    cursor.execute(add_csv_file, csv_info) 
    File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 393, in execute 
    self._handle_result(self._connection.cmd_query(stmt)) 
    File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 586, in cmd_query 
statement)) 
    File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 508, in _handle_result 
    raise errors.get_exception(packet) 
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/Users/afink/Documents/Research/Current Papers & Books/Youth Assessm' at line 1 

的輸出字符串之前執行在MySQL命令行接口工作在同一個用戶下的罰款。

似乎它應該是一個簡單的問題,但我卡住了!

def db_connect(): 
    config = { 
     'user': 'username', 
     'password': 'pass', 
     'host': 'localhost', 
     'database': 'uscensus', 
     'raise_on_warnings': True, 
    } 

    from mysql.connector import errorcode 
    try: 
     cnx = mysql.connector.connect(**config) 
     return cnx 
    except mysql.connector.Error as err: 
     if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: 
     print("Something is wrong with your user name or password") 
     elif err.errno == errorcode.ER_BAD_DB_ERROR: 
     print("Database does not exist") 
     else: 
     print(err) 
    else: 
     cnx.close() 


cursor = cnx.cursor() 

os.chdir("./") 
for files in glob.glob("*.csv"): 

    add_csv_file = ('''load data local infile '%(cwd)s/%(file_name)s' into table %(table_name)s 
        columns terminated by ',' 
        optionally enclosed by '\"' 
        escaped by '\"' 
        lines terminated by '\\n';''') 

    # Insert CSV file information 
    csv_info = { 
     'cwd': os.getcwd(), 
     'file_name': files, 
     'table_name': 'SF1_' + files[2:-8], 
    } 


    print add_csv_file % csv_info # Temporary Debug 

    cursor.execute(add_csv_file, csv_info) 

# Make sure data is committed to the database 
cnx.commit() 
cursor.close() 
cnx.close() 

非常感謝您的幫助!

回答

3

這是很容易與您的網絡連接添加如下相應的客戶端標誌解決

LoadSQL = """LOAD DATA LOCAL INFILE '%s' 
    INTO TABLE %s 
    FIELDS TERMINATED BY '\t' 
    LINES TERMINATED BY '\n' 
    IGNORE 1 LINES 
    (field1, field2, field3, field4)""" % (csvfile, tabl_name) 
cursor.execute(LoadSQL) 
cnx.commit() 
cursor.close() 
cnx.close() 
0

您調用execute()方法時,有一個錯誤:

cursor.execute(add_csv_file, csv_info)

嘗試:

cursor.execute(query, (add_csv_file, csv_info,))

+0

這並不適用於我 - 我沒有確定的「查詢」字符串。 我用過的方法似乎在這裏工作: http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html –

1

好了,這裏就是我已經想通了。

@Dd_tch - 你的回答讓我意識到,這個代碼將更好的工作:雖然MySQL的站點連接器似乎表明,你可以做我在做什麼(http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html

query = add_csv_file % csv_info 
cursor.execute(query) 

,這不是加工。

當我固定的,我得到了一個新的錯誤: mysql.connector.errors.ProgrammingError:1148(42000):所使用的命令是不允許使用此版本的MySQL

有幾個網站表明,這可以通過在MySQL Connector配置中添加「local_infile = 1」來解決。這裏有一個:http://dev.mysql.com/doc/refman/5.1/en/loading-tables.html

這個解決方案並不適合我。我的local_infile在MySQL上設置爲1,我無法在Python代碼中設置它,或者我得到一個電子郵件地址我將使用某些將逐行讀取CSV文件的LOAD LOCAL DATA INFILE來代替LOAD LOCAL DATA INFILE並將行插入數據庫。這將使代碼更容易移植到其他數據庫。

import mysql.connector 
from mysql.connector.constants import ClientFlag 

cnx = mysql.connector.connect(user='[username]', password='[pass]', host='[host]', client_flags=[ClientFlag.LOCAL_FILES]) 
cursor = cnx.cursor() 

這將給權限MySQL來訪問你的機器上的本地文件,然後將下面的LOAD將工作: