Load file into Database
<p>I am trying to load a csv file into the database but no success. The issue is that it works when I load the query directly in the mysql shell but it doesn't work when I tried doing from the python code. The code I am using is shown below:</p>

<pre><code>db = connect_to_database()
cursor = db.cursor()

q = "LOAD DATA LOCAL INFILE '/home/ubuntu/load_file.csv' INTO TABLE test_load FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'"
db.query(q)
cursor.execute(q)
db.commit()
</code></pre>

<p>The error it throws says:</p>

<pre><code>File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 281, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1148, 'The used command is not allowed with this MySQL version')
</code></pre>

<p>When I copied the string into the sql shell, it loads successfully. I'm not sure why its throwing this error.</p>

<p>So I managed to solve the problem. All it was missing was the local_infile parameter in the MySQLDB connect:</p>

<p>db=MySQLdb.connect(host=raw_host,user=raw_user,passwd=raw_passwd,db=raw_db, local_infile = 1)</p>

<p>When I added this parameter, it worked as expected. Thank you all for your efforts in resolving the problem.</p>

python mysql