Python/MySQL-加载数据本地文件 [英] Python/MySQL - LOAD DATA LOCAL INFILE

查看:167
本文介绍了Python/MySQL-加载数据本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将mysql连接器用于Python,并且试图通过Python(Windows)运行以下SQL语句-这是一个.csv文件:

I am using the mysql connector for Python and I'm trying to run the following SQL statement via Python (Windows) - It's a .csv file:

sql1 = ('SET GLOBAL local_infile = "ON";')
cursor.execute(sql1)

sql2 = ('LOAD DATA LOCAL INFILE "' + path[1:-1] + '" INTO TABLE mytable COLUMNS  TERMINATED BY "," LINES TERMINATED BY "\\r\\n" (COL0, COL1, COL2, COL3, COL4, COL5, COL6) SET COL7 = "'some_data'";')
cursor.execute(sql2)

但是当我尝试执行时,我收到以下异常:

but when I try to execute I receive the following exception:

1148(42000):此MySQL版本不允许使用所使用的命令

1148 (42000): The used command is not allowed with this MySQL version

如果我尝试在mysql控制台上执行LOAD DATA LOCAL INFILE,则一切运行正常.

If I try to execute LOAD DATA LOCAL INFILE on mysql console, everything runs fine.

推荐答案

默认情况下,使用连接器/Python禁用加载数据文件 在创建连接集LOCAL_FILES客户端标志时,如下所示:

Load Data Infile is disabled by default with Connector/Python while creating the connection set LOCAL_FILES client flag like this:

from mysql.connector.constants import ClientFlag
conn = mysql.connector.connect(...., client_flags=[ClientFlag.LOCAL_FILES])

这篇关于Python/MySQL-加载数据本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆