Python2.7 MySQL Connector Error on LOAD DATA LOCAL INFILE

后端 未结 4 725
我寻月下人不归
我寻月下人不归 2021-01-14 08:26

I\'m trying to dynamically load census data into a mysql database (from .csv files) using Python and MySQL Connector.

I can\'t figure out why I am getting the error

4条回答
  •  失恋的感觉
    2021-01-14 08:54

    This is easily solved with adding the appropriate client flag in your connection as below:

    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()
    

    This will give permission for MySQL to access the local files on your machine and then the following LOAD will work:

    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()
    

提交回复
热议问题