# How to use fastcsv?

**URL:** <https://www.ruby-forum.com/t/how-to-use-fastcsv/127696>\
**Category:** Rails\
**Created:** [January 31, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/how-to-use-fastcsv/127696 "2008-01-31T14:29:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [January 31, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/how-to-use-fastcsv/127696/1 "2008-01-31T14:29:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [February 8, 2008, 12:54am UTC](https://www.ruby-forum.com/t/how-to-use-fastcsv/127696/2 "2008-02-08T00:54:07Z")

</div>

My results aren’t much better ☹

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

---

<div class="post-metadata">

**Author:** ![thiel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@thiel](https://www.ruby-forum.com/u/thiel)\
**Post date:** [February 8, 2008, 5:51am UTC](https://www.ruby-forum.com/t/how-to-use-fastcsv/127696/3 "2008-02-08T05:51:30Z")

</div>

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

RTFM at:  
[http://fastercsv.rubyforge.org/classes/FasterCSV.html](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
