Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > DB to CSV

Reply
Thread Tools

DB to CSV

 
 
Mitja Čebokli
Guest
Posts: n/a
 
      04-16-2010
Hello,

i am very new to ruby and i have a major problem i would like to have
some help on...

I would like to execute a query on a database and export the values to
the CSV file. I have tried many different ways and i can't get it to
work.

This is what i have so far:

require 'oci8'
require 'csv'

time = Time.now
rows =[]

connection = OCI8.new("user", "pass", "srv")
dataset = connection.exec("select column from table")
while r = dataset.fetch()
rows << r.join("\n")
end
dataset.close
connection.logoff

CSV.open("c:\\Temp\\test_"+time.strftime("%d-%m-%Y_%H-%M")+".csv","w",:col_sep
=> ";") do |csv|

rows.each do |row|
csv << row
end
end

I am getting this error:
csv.rb:1700 undefined method 'map'

What i want to achieve is pretty simple:
- have a header with names of columns from the database (automatic)
- write a CSV file with each record as a new line
- have a semicolon as a seperator

Thanks for any info on this.
BTW: no RAILS!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      04-16-2010
[Note: parts of this message were removed to make it a legal post.]

Does something like this work?

require 'csv'

time = Time.now
rows =[]

(1..10).each do |x|
rows << ['row', x, 'cheese']
end

CSV.open("testfile.csv","w",:col_sep => ";") do |csv|
rows.each do |row|
csv << row
end
end

It this works then look at your data.

Also I note that the line

rows << r.join("\n")

is probably not what you want. It will turn an array into a string.

a = ['row',1,'cheese']
a.join("\n") => "row\n1\ncheese"

Are you sure you want to do this?

 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      04-16-2010
[Note: parts of this message were removed to make it a legal post.]

Further hacking reveals that this might be a better option to create the
file.

CSV.open("testfile.csv","w", ";") do |csv|

Note we are no longer using the :col_sep => ";" notation but we do get the ;
as a record / column separator.

 
Reply With Quote
 
Mitja Čebokli
Guest
Posts: n/a
 
      04-16-2010
Peter Hickman wrote:
> Further hacking reveals that this might be a better option to create the
> file.
>
> CSV.open("testfile.csv","w", ";") do |csv|
>
> Note we are no longer using the :col_sep => ";" notation but we do get
> the ;
> as a record / column separator.


Hi, thanks a lot for your help.

I have changed

while r = dataset.fetch()
rows << r.join("\n")
end

to

while r = dataset.fetch()
rows << r
end

and this now works as expected! No more problems about filling the CSV
file.

On the other hand, not using :col_sep is giving me an error:
can't convert String into Integer (TypeError)

Maybe the formatting of the line is wrong?

One more question on this matter.
How do I extract/use the table columns from the select file or if used
as "select *" to extract all the column names, and than use them as a
CSV header.

Thanks again helping me out, really appreciate it!

BR



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Mitja Čebokli
Guest
Posts: n/a
 
      04-16-2010
And one more thing

I have a problem, as it seems, with columns where data are numbers.
For example, row with ID number is returning me something like 0.2001E4

What's with that? Shouldn't the CSV export treat all the data coming
from DB as a string? Maybe there is a problem with wrong oci8 usage? (on
my side of course)

Thxs
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      04-16-2010
On 16 April 2010 12:57, Mitja =C4=8Cebokli <> wrot=
e:

> On the other hand, not using :col_sep is giving me an error:
> can't convert String into Integer (TypeError)
>
> Maybe the formatting of the line is wrong?
>
>

Well if :col_sep works for you then use it. Perhaps this is a gem version
issue. It didn't like it for me.

One more question on this matter.
> How do I extract/use the table columns from the select file or if used
> as "select *" to extract all the column names, and than use them as a
> CSV header.
>
>

Sorry I have no knowledge of the Oracle db driver so I can't help you with
that.

I have a problem, as it seems, with columns where data are numbers.
> For example, row with ID number is returning me something like 0.2001E4
>


How is the data being returned from the database? If Oracle is returning th=
e
data in this format 0.2001E4 (as a string) then ruby will just copy it as
is. I suspect that Oracle is returning the float as a string because ruby
would display 0.2001E4 as 2001.0 if it was actually given as a number.

You might need to convert the data to get the values displayed in a sensibl=
e
way.

a =3D "0.2001E4" =3D> "0.2001E4"
a.to_f =3D> 2001.0
a.to_f.to_s =3D> "2001.0"

 
Reply With Quote
 
KUBO Takehiro
Guest
Posts: n/a
 
      04-16-2010
On Fri, Apr 16, 2010 at 9:02 PM, Mitja =C4=8Cebokli
<> wrote:
> And one more thing
>
> I have a problem, as it seems, with columns where data are numbers.
> For example, row with ID number is returning me something like 0.2001E4


How did you define the ID number.
If it is defined as NUMBER, the column is not an integer and it may not fit=
to
Float. Thus ruby-oci8 fetch it as BigDecimal by default.
See: http://rubyforge.org/forum/forum.php...orum_id=3D107=
8

Could you redefine the ID as NUMBER(3 or so?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to parse a csv...... a csv that has CRLF in the fields sso Java 20 04-26-2009 11:04 AM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Re: csv writerow creates double spaced excel csv files Skip Montanaro Python 0 02-13-2004 08:50 PM
csv writerow creates double spaced excel csv files Michal Mikolajczyk Python 0 02-13-2004 08:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57