Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOAD DATA LOCAL INFILE sqlalchemy and python to mysql db

I'm trying to use LOAD LOCAL DATA INFILE with sqlalchemy to upload batch files to my server. I have edited my /etc/mysql/my.conf to contain local-infile == 1 under both [mysql] and [mysqld], yet I'm getting this traceback when I run the program.

The relevant line of code is:

    Traceback (most recent call last):
  File "main.py", line 48, in <module>
    con.execute(sql)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 906, in execute
    return self._execute_text(object, multiparams, params)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1054, in _execute_text
    statement, parameters
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
    context)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1332, in _handle_dbapi_exception
    exc_info
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
    context)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 442, in do_execute
    cursor.execute(statement, parameters)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/Users/eoddata/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1148, 'The used command is not allowed with 
this MySQL version') [SQL: "LOAD DATA LOCAL
 INFILE 'stocks/AMEX_20150420.txt' INTO TABLE database.new_table\n                    
FIELDS TERMINATED BY ',' (Symbol,@Date,Open,High,Low,Close,Volume)\n                    
SET Date = STR_TO_DATE(@Date,'%%Y%%m%%d');"]

I've done some research from the docs and can't find a way to fix this error. With python MySQLdb (from pip install MySQL-Python) will allow me to pass in a local-infile paramter. I can't find an equivalent in SQLAlchemy. Any help is appreciated.

EDIT: I've worked on this all night and still haven't made any progress - a lot of dead ends. Any ideas?

Thanks, Jared

like image 339
Jared Avatar asked Dec 24 '22 20:12

Jared


1 Answers

You should add local_infile=1 to the connection string.

SQLALCHEMY_DATABASE_URI = "mysql://user:pass@localhost/dbname?charset=utf8&local_infile=1"
like image 116
imo Avatar answered Dec 27 '22 11:12

imo