LoginSignup
2
2

More than 5 years have passed since last update.

PyMysqlでLOAD DATAする

Last updated at Posted at 2017-07-24

下記のようにpymysqlでLOAD DATAを実行しようとしたところ、

def load_data(file_path):
    with conn.cursor() as cursor:
        sql = """
LOAD DATA LOCAL INFILE '{}' 
INTO TABLE Sample 
CHARACTER SET utf8 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES 
        """.format(file_path)
        cursor.execute(sql)

以下のエラーが出て取り込めませんでした。

pymysql.err.InternalError: (1148, 'The used command is not allowed with this MySQL version')

connect時に local_infile=True を指定することで、解消しました。

pymysql.connect(
    :
    local_infile=True
)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2