MySQL LOAD DATA LOCAL INFILE Python

后端 未结 3 652
无人共我
无人共我 2020-12-08 21:04

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 D         


        
相关标签:
3条回答
  • 2020-12-08 21:53

    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

    0 讨论(0)
  • 2020-12-08 21:56

    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
    
    0 讨论(0)
  • 2020-12-08 22:02

    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)
    
    0 讨论(0)
提交回复
热议问题