# Looking For A Better Way To Do This

**URL:** <https://www.ruby-forum.com/t/looking-for-a-better-way-to-do-this/105327>\
**Category:** Rails\
**Created:** [May 28, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/looking-for-a-better-way-to-do-this/105327 "2007-05-28T21:39:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [May 28, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/looking-for-a-better-way-to-do-this/105327/1 "2007-05-28T21:39:08Z")

</div>

For efficiency sake after a bunch of experimentation I have come to  
the conclusion, for me, that the best way to export 100K plus rows  
into csv format is by breaking into mysql and doing an OUTFILE. It is  
easy except for one thing… managing quotes and double quotes.

Here is an example of what I need to do:

system(“echo ‘SELECT region\_name, area\_name,  
CONCAT('=\"',district\_number,'\"') FROM jobs LIMIT 1000 INTO  
OUTFILE "/temp/#{file\_name}";’ | mysql  
-u#{ActiveRecord::Base.configurations[‘development’][‘username’]}  
#{ActiveRecord::Base.configurations[‘development’][‘database’]}”)

The problem is the CONCATs and stuff like that I am going to have to  
do to format the export properly:  
CONCAT('=\"',district\_number,'\"')

The CONCAT in this case does not even work. If I go into the mysql  
command prompt the following though does work:  
SELECT region\_name, area\_name, CONCAT(‘="’,district\_number,‘"’) FROM  
jobs LIMIT 1000 INTO OUTFILE “/temp/test.xls”;

I can’t manage all the escaping I might need or not need.

Their must be a better way to do this? Can I put the mysql statement  
into a file or something so I can bypass all the quote problems?

Please help? 🙂

Your Friend,

John

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)
