为什么要在sqlalchemy中设置local_infile=1来加载本地文件? sqlalchemy 中不允许加载文件问题

why should we set the local_infile=1 in sqlalchemy to load local file? Load file not allowed issue in sqlalchemy

我正在使用 sqlalchemy 连接到 MySQL 数据库,发现一个奇怪的行为。 如果我查询

LOAD DATA LOCAL INFILE 
'C:\\Temp\\JaydenW\\iata_processing\\icer\\rename\\ICER_2017-10- 
12T09033
7Z023870.csv    

弹出错误:

sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1148, u'The used 
command is not allowed with this MySQL versi
on') [SQL: u"LOAD DATA LOCAL INFILE 
'C:\\Temp\\JaydenW\\iata_processing\\icer\\rename\\ICER_2017-10- 
12T090337Z023870.csv' INTO TABLE genie_etl.iata_icer_etl LINES TERMINATED BY 
'\n' 
IGNORE 1 Lines   (rtxt);"] (Background on this error at: 
http://sqlalche.me/e/2j85)

我发现原因是: 我需要将参数设置为

args = "mysql+pymysql://"+username+":"+password+"@"+hostname+"/"+database+"? 
local_infile=1"

如果我用MySQL官方连接库。我不需要这样做。

myConnection = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database)

任何人都可以帮助我了解这两种机制之间的区别吗?

原因是这些机制使用不同的驱动程序。 在 SQLAlchemy 中,您似乎正在使用 pymysql engine, which uses the PyMySQL Connection class 创建数据库连接。如果用户想使用 LOAD DATA LOCAL 命令,则要求用户显式传递 local_infile 参数。

另一个示例使用 MySQLdb, which is basically a wrapper around the MySQL C API (and to my knowledge not the official connection library; that would be MySQL Connector Python, which is also available on SQLAlchemy as mysqlconnector)。这显然以默认启用 LOAD DATA LOCAL 的方式创建连接。