# Very basic question | Ruby + MySql

**URL:** <https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657>\
**Category:** Ruby\
**Created:** [May 5, 2008, 9:41am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657 "2008-05-05T09:41:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 5, 2008, 9:41am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/1 "2008-05-05T09:41:07Z")

</div>

I’m sorry to ask such a newbie question but I wasn’t able to figure  
this out yet. I just want to write a simple Ruby program that can  
select data from a MySql database. I assume that Ruby DBI would be the  
easiest way to go but I don’t think I have the required modules/gems  
installed. I can’t even do a require ‘dbi’, it throws the following  
error: LoadError: no such file to load – dbi

I found a few tutorial pages but they don’t give all the steps. These  
are the pages I used as reference:

[http://www.tmtm.org/en/ruby/mysql/](http://www.tmtm.org/en/ruby/mysql/)

[http://www.kitebird.com/articles/ruby-mysql.html](http://www.kitebird.com/articles/ruby-mysql.html)

Basically I am looking for the step-by-step instructions on how to run  
a query on a MySql DB using a Ruby program from thin air. My system is  
a Mac OSX Leopard with ruby 1.8.6 (2007-09-24 patchlevel 111)  
[universal-darwin9.0].

Can someone kindly give me the exact steps (from installing the  
modules to using Ruby DBI) to run the simplest possible select query  
form MySql DB? Again, sorry for the newbie question.

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 5, 2008, 9:45am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/2 "2008-05-05T09:45:48Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Csongor wrote:  
| I’m sorry to ask such a newbie question but I wasn’t able to figure  
| this out yet. I just want to write a simple Ruby program that can  
| select data from a MySql database. I assume that Ruby DBI would be the  
| easiest way to go but I don’t think I have the required modules/gems  
| installed. I can’t even do a require ‘dbi’, it throws the following  
| error: LoadError: no such file to load – dbi

are you using require ‘rubygems’ on the top of your script (before you  
load DBI)?

If not, try that.

(‘export RUUBYOPTS=rubygems’ should fix that permanently, I think)

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ - You know you’ve been hacking too long when…  
…your radio is playing, and you’re starting a big compile on your home  
box, and it occurs to you that the music will begin to get a bit jerky,  
because you think it’s playing through the computer and you’re not quite  
sure how well it’ll multitask with the compile.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgeuvMACgkQbtAgaoJTgL/pSgCfYwR/Gvxe1/7hmPkOoxYflxoZ  
TeUAoIgALUM/bpUPn5Da5nQyHP6fZILy  
=IgRK  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 5, 2008, 9:54am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/3 "2008-05-05T09:54:41Z")

</div>

Hey,  
Well there may be a couple of reons involved why you are getting the  
error.  
Please check with your installtion of ruby gems.  
Once your done check wether ‘gem’ command is working or not in your  
terminal.  
If something goes wrong with the installtion then you can make it with  
up  
the dependencies.  
once you are done with the process…In your irb please check with the  
‘require dbi’ .If it returns true then you are ready to go ahead with  
the  
mysql.

cheers,  
kranthi

On Mon, May 5, 2008 at 1:15 PM, Phillip G. \<

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 6, 2008, 7:21am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/4 "2008-05-06T07:21:16Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Csongor wrote:  
| Philip,  
|  
| Thanks for the response. I wasn’t, but I just did a quick test and  
| require ‘rubygems’ is giving me a false. Look:  
|  
| sh-3.2# ruby -v  
| ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]  
| sh-3.2# gem -v  
| 1.1.1  
| sh-3.2# irb  
|\>\> require ‘rubygems’  
| =\> false  
|  
| Any ideas?

Well, it looks like you didn’t install Ruby-DBI, since ‘dbi’ is what you  
need for the require.

Fortunately, the RDoc files for Ruby-DBI show you the installation:

[http://ruby-dbi.rubyforge.org/rdoc/index.html](http://ruby-dbi.rubyforge.org/rdoc/index.html)

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ - You know you’ve been hacking too long when…  
…you start dreaming code.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgf6qUACgkQbtAgaoJTgL/25QCfWmCWsHysuQJHbNm/TpmULEVt  
legAn0wn6R+ClVOySgQyHouH09O0HJT9  
=A49m  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 5, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/5 "2008-05-05T22:36:03Z")

</div>

Philip,

Thanks for the response. I wasn’t, but I just did a quick test and  
require ‘rubygems’ is giving me a false. Look:

sh-3.2# ruby -v  
ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]  
sh-3.2# gem -v  
1.1.1  
sh-3.2# irb

> > require ‘rubygems’  
> > =\> false

Any ideas?

On May 5, 12:45 am, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 6, 2008, 10:31am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/6 "2008-05-06T10:31:10Z")

</div>

Yeah I tried to instal dbi but this is what I get:

csongors-mac:dbi-0.2.0 csgyuricza$ ruby setup.rb config  
entering config phase…  
config done.  
csongors-mac:dbi-0.2.0 csgyuricza$ ruby setup.rb setup  
entering setup phase…  
setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
Versions/1.8/usr/bin/ruby”  
setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
Versions/1.8/usr/bin/ruby”  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby  
extconf.rb  
can’t find header files for ruby.  
setup failed  
‘system /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/  
ruby extconf.rb’ failed  
try “ruby setup.rb --help” for usage  
csongors-mac:dbi-0.2.0 csgyuricza$ sudo ruby setup.rb setup  
Password:  
entering setup phase…  
setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
Versions/1.8/usr/bin/ruby”  
setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
Versions/1.8/usr/bin/ruby”  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby  
extconf.rb  
can’t find header files for ruby.  
setup failed  
‘system /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/  
ruby extconf.rb’ failed  
try “ruby setup.rb --help” for usage

On May 5, 10:20 pm, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 6, 2008, 8:29am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/7 "2008-05-06T08:29:59Z")

</div>

Hi CSongor,

Try this. I am working windows here.I think it will work in Mac also…

TRY.rb

* * *

=begin  
OS : Windows here (May be same MAC OSx)  
Ruby : 1.8.6  
DB :Mysql  
Gem :\> gem install mysql  
=end

require “mysql”  
#require mysql gem  
connection = Mysql.connect(“localhost”, “root”, “”, “cdcol”)  
#localhost = hostname  
#root = username of mysql DB  
#“”=password of mysql DB  
#cdcol = DB name  
output = connection.query(“SELECT \* FROM cds;”)  
#cds = table name  
output.each do |x|  
puts x.inspect  
end

* * *

Regards,  
P.Raveendran  
Railsfactory

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![Csongor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@Csongor](https://www.ruby-forum.com/u/Csongor)\
**Post date:** [May 6, 2008, 11:36am UTC](https://www.ruby-forum.com/t/very-basic-question-ruby-mysql/136657/8 "2008-05-06T11:36:00Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

A: Because it makes it difficult to follow the conversation.  
Q: Why is top posting bad?

Csongor wrote:  
| Yeah I tried to instal dbi but this is what I get:  
|  
| csongors-mac:dbi-0.2.0 csgyuricza$ ruby setup.rb config  
| entering config phase…  
| config done.  
| csongors-mac:dbi-0.2.0 csgyuricza$ ruby setup.rb setup  
| entering setup phase…  
| setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
| Versions/1.8/usr/bin/ruby”  
| setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
| Versions/1.8/usr/bin/ruby”  
| /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby  
| extconf.rb  
| can’t find header files for ruby.  
| setup failed  
| ‘system /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/  
| ruby extconf.rb’ failed  
| try “ruby setup.rb --help” for usage  
| csongors-mac:dbi-0.2.0 csgyuricza$ sudo ruby setup.rb setup  
| Password:  
| entering setup phase…  
| setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
| Versions/1.8/usr/bin/ruby”  
| setting #! line to “#!/System/Library/Frameworks/Ruby.framework/  
| Versions/1.8/usr/bin/ruby”  
| /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby  
| extconf.rb  
| can’t find header files for ruby.  
| setup failed  
| ‘system /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/  
| ruby extconf.rb’ failed  
| try “ruby setup.rb --help” for usage

Well, without the header-files for Ruby and the DB engines you want to  
use, you won’t be able to compile the extensions.

Install those, and try again.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ There’s never enough time to do all the nothing you want.  
– Calvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkggJlYACgkQbtAgaoJTgL8PwgCgiC/fF+mEFAmlyeD1uKn3zu4F  
odAAn29nWVuxq6yZwafzSjfHYkk/A4jQ  
=xm1c  
-----END PGP SIGNATURE-----
