Using MySQLdb to select into the local file

Nikhil mnikhil at gmail.com
Thu May 22 15:13:04 EDT 2008


I am using the MySQLdb python module. I have a table named 'testing' 
with few columns, under the 'test' database, what is hosted on a remote 
mysql server.

I want to run the following query to get a comma-seperated information 
from the table


LOCK TABLES foo READ;
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
   FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
   LINES TERMINATED BY '\n'
   FROM 'testing'
UNLOCK TABLES;

..the query is running fine, but what I am noticing is /tmp/result.txt 
is getting created locally on a mysqld running machine but not on the 
client(python program) using the MySQLdb module.
I am wondering if anyone has gone through this before and made some 
arrangements to iterate over the data but get the /tmp/result.txt 
generated locally on the client machine?

On the local or the server file, from 
http://dev.mysql.com/doc/refman/5.0/en/select.html

---
  The SELECT ... INTO OUTFILE statement is intended primarily to let you 
very quickly dump a table to a text file on the server machine. If you 
want to create the resulting file on some client host other than the 
server host, you cannot use SELECT ... INTO OUTFILE. In that case, you 
should instead use a command such as mysql -e "SELECT ..." >  file_name 
to generate the file on the client host.
---

So, what is the equivalent of using '-e' mysql commandline option in the 
MySQLdb python module?

I am sorry if this is supposed to go to only MySQL, but not really sure 
so copying the relevant assumed.

Thanks,
Nikhil



More information about the Python-list mailing list