Very basic question | Ruby + MySql

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.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.

-----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
Blog: 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

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

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. <

-----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


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: 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

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

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. [email protected]

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. [email protected]

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

-----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
Blog: 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

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