Very basic question | Ruby + MySql

Discussion in 'Ruby' started by Csongor, May 5, 2008.

  1. Csongor

    Csongor Guest

    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.
    Csongor, May 5, 2008
    #1
    1. Advertising

  2. -----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 Gawlowski
    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-----
    Phillip Gawlowski, May 5, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    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 Gawlowski <
    > wrote:

    > -----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 Gawlowski
    > 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-----
    >
    >
    kranthi reddy, May 5, 2008
    #3
  4. Csongor

    Csongor Guest

    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 Gawlowski <>
    wrote:
    > -----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 Gawlowski
    > 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-----
    Csongor, May 5, 2008
    #4
  5. -----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 Gawlowski
    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-----
    Phillip Gawlowski, May 6, 2008
    #5
  6. 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
    http://raveendran.wordpress.com

    --
    Posted via http://www.ruby-forum.com/.
    Raveendran Jazzez, May 6, 2008
    #6
  7. Csongor

    Csongor Guest

    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 Gawlowski <>
    wrote:
    > -----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 Gawlowski
    > 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-----
    Csongor, May 6, 2008
    #7
  8. -----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 Gawlowski
    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-----
    Phillip Gawlowski, May 6, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    451
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    334
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,592
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    498
    Dave Thompson
    Feb 14, 2005
  5. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,190
    August Karlstrom
    Sep 26, 2008
Loading...

Share This Page