user1746082
user1746082

Reputation: 230

MySQL LOAD DATA LOCAL INFILE Python

I am running Ubuntu 12.04 and MySQL 5.5 Alright so here is the problem:

Using the MySQLDB module for Python, the SQL command:

cursor.execute("LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE 'example_mysql_table' TERMINATED BY ',';")

Doesn't work. I get ERROR 1148: The used command is not allowed with this MySQL version

I have looked around for a solution for this for a while now, and so far it seems other people having the same problem have fixed it by adding "local-infile = 1" to 'my.cnf' underneath [mysqld] and [mysql]. This has not worked for me, and I am not sure why.

Relevant Link:

MySQL: Enable LOAD DATA LOCAL INFILE

Upvotes: 22

Views: 14554

Answers (3)

stoicalpirate
stoicalpirate

Reputation: 358

I know this is a really old thread, but I thought I'd just add a little footnote regarding Flask.

I was unable to upload any csv files to MySQL via LOAD DATA INFILE, so I attempted to use LOAD DATA LOCAL INFILE. I got the same 'ERROR 1148: The used command is not allowed with this MySQL version' mentioned above.

My solution to this was to open up flaskext > mysql.py and modify the 'connect' function. I added:

if self.app.config['MYSQL_LOCAL_INFILE']:
    self.connect_args['local_infile'] = self.app.config['MYSQL_LOCAL_INFILE']

I then added:

app.config['MYSQL_LOCAL_INFILE'] = True

to my flask module. This effectively allows for the local_infile option of pymysql to be set to 'True' when using flaskext.mysql

Upvotes: 0

Johann Bosman
Johann Bosman

Reputation: 713

After spending hours on trying all kinds of settings in the config files and restarting mysql dozens of times I came up with this which seems to solve the problem (could not find this in any documentation anywhere)

MySQLdb.connect(server, username, password, database, local_infile = 1)

Upvotes: 69

Devart
Devart

Reputation: 121902

As I see, there is no file option local-infile. But you can change this value dynamically in a script.

To view this option run this query -

SELECT @@global.local_infile;

To set variable use this statement -

SET @@global.local_infile = 1;

Tested in MySQL command line console, everything is OK:

SET @@GLOBAL.local_infile = 1;
LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE table1;
Query OK, 3 rows affected (0.06 sec)

SET @@GLOBAL.local_infile = 0;
LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE table1;
ERROR 1148 (42000): The used command is not allowed with this MySQL version

Upvotes: 4

Related Questions