How to use fastcsv?

Hi,

I am a newbie and I want to export an excel-file using fastcsv.

This is an example program from book Practical Ruby Gems:

=================
#!/usr/local/bin/ruby -w
#fastercsv.rb

Created by James Edward G. II on 2006-04-01.

Copyright 2006 Gray Productions. All rights reserved.

This file is just another name for faster_csv.rb.

require “faster_csv”
csvdata = “jones,bob,165\n”
csvdata = “smith,tim,10\n”
csvdata = “doe,john,155\n”
lastname, firstname, iq = “”
fastercsv.parse(csvdata) do |row |
lastname, firstname, iq = *row
puts “#{firstname} #{lastname} has an IQ of #{iq}”
end

I am using Netbeans and I got the following message:

C:/NetBeansProjects/Fastercsv/lib/main.rb:17: undefined local variable
or method `fastercsv’ for main:Object (NameError)

Can you suggest how to solve this problem?

Thanks in andvance,

Thiel

My results aren’t much better :frowning:

thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ ruby import.rb
import.rb:9: undefined local variable or method `fastercsv’ for
main:Object (NameError)
thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ cat import.rb
require ‘fastercsv’

csvdata = “one\n”
csvdata = “two\n”
csvdata = “three\n”

one = “”

fastercsv.parse(csvdata) do |row |
one = *row
end
thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ mysql -u root -ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.0.44-log Gentoo Linux mysql-5.0.44

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> DESCRIBE csv_data.one;
±------±--------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±--------±-----±----±--------±------+
| one | int(11) | YES | | NULL | |
±------±--------±-----±----±--------±------+
1 row in set (0.26 sec)

mysql> quit
Bye
thufir@arrakis ~/csv $

-Thufir

The following runs without error and generates output :slight_smile:
However, it doesn’t import data to the db yet :frowning:

RTFM at:
http://fastercsv.rubyforge.org/classes/FasterCSV.html

the code, data and db:

thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ ruby import.rb
[“one”]
[“two”]
[“three”]
thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ cat import.rb
require ‘rubygems’
require ‘fastercsv’

infile = “data.csv”

FCSV.foreach(infile) do |row|
p row
end
thufir@arrakis ~/csv $
thufir@arrakis ~/csv $ sqlite3 foo.sqlite3
SQLite version 3.4.1
Enter “.help” for instructions
sqlite> .schema
CREATE TABLE foos (bar STRING);
CREATE TABLE schema_info (version integer);
sqlite>
sqlite> SELECT * FROM foos;
sqlite> .quit
thufir@arrakis ~/csv $

-Thufir