[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using DBI and MySQL gems

Owein Herrmann

10/13/2008 9:41:00 PM

Quick question:

I thought using rubygems that to use the dbi and mysql to access a MySQL
database from Ruby I would only have to do the following:

gem 'mysql'
gem 'dbi'

and then I could do whatever, eg:

dbh = DBI.connect('DBI:Mysql:money', 'mysqluser', 'pass3')

HOWEVER, what I have to do is:

gem 'mysql'
gem 'dbi'
require 'dbi'

and THEN the DBI.connect syntax works.... does this indicate some
problem with my rubygems (or dbi) installation?

$ gem env
RubyGems Environment:
- VERSION: 0.9.4 (0.9.4)
- INSTALLATION DIRECTORY: c:/ruby/lib/ruby/gems/1.8
- GEM PATH:
- c:/ruby/lib/ruby/gems/1.8
- REMOTE SOURCES:
- http://gems.rub...

TIA for any help
~Owein
--
Posted via http://www.ruby-....

12 Answers

Erik Hollensbe

10/15/2008 7:20:00 AM

0

Owein Herrmann wrote:
> Quick question:
>
> I thought using rubygems that to use the dbi and mysql to access a MySQL
> database from Ruby I would only have to do the following:
>
> gem 'mysql'
> gem 'dbi'
>
> and then I could do whatever, eg:
>
> dbh = DBI.connect('DBI:Mysql:money', 'mysqluser', 'pass3')
>
> HOWEVER, what I have to do is:
>
> gem 'mysql'
> gem 'dbi'
> require 'dbi'

Unless rubygems has (recently) changed, the 'gem' command doesn't issue
any requires, it just modifies the load path to point at your gem.

I don't know if this is recommended usage anymore, but provided you
haven't installed dbi with the provided setup.rb, "require 'dbi'" should
work without the gem statements.

-Erik
--
Posted via http://www.ruby-....

Owein Herrmann

10/15/2008 4:16:00 PM

0

Erik Hollensbe wrote:
>
> Unless rubygems has (recently) changed, the 'gem' command doesn't issue
> any requires, it just modifies the load path to point at your gem.
>
> I don't know if this is recommended usage anymore, but provided you
> haven't installed dbi with the provided setup.rb, "require 'dbi'" should
> work without the gem statements.
>
> -Erik

Hmmmm... that definitely does not work. When I say "require 'dbi'" it
returns true, but then dbh = DBI.connect('DBI:Mysql:money', 'mysqluser',
'pass3') causes an exception about DBI not being defined... I'm not on
my ruby box right now, so I cannot give the exact notation... basically,
what I show above works, the two gem commands and then the require.

In the pickaxe, pg 221 (2nd ed) it says I need to say

require 'rubygems'
require_gem '<gem_name>'

but rubygems appears to be required automatically when I start up, and
require_gem says that I should use 'gem' instead. So, if require
'rubygems' is not required (haha) and "require_gem" is just "gem", I'm
back to my original question, should I not be able to just say

gem 'dbi'

and, incidently, if I installed dbi using the gem installer, and you say
I should be able to "require 'dbi'" without the gem statements, why does
that not work? Do I have to do something special to get things that were
loaded as gems to be recognized differently than what I do for regular
packages?
--
Posted via http://www.ruby-....

Erik Hollensbe

10/15/2008 5:05:00 PM

0

Owein Herrmann wrote:
>> I don't know if this is recommended usage anymore, but provided you
>> haven't installed dbi with the provided setup.rb, "require 'dbi'" should
>> work without the gem statements.
> Hmmmm... that definitely does not work. When I say "require 'dbi'" it
> returns true, but then dbh = DBI.connect('DBI:Mysql:money', 'mysqluser',
> 'pass3') causes an exception about DBI not being defined... I'm not on
> my ruby box right now, so I cannot give the exact notation... basically,
> what I show above works, the two gem commands and then the require.

Is this 1.9? I'm not sure how rubygems would be automatically required
in 1.8.x. I guess it's also possible that the require overrides have
been removed from rubygems. require_gem has been deprecated for some
time and probably has been removed by now.

Well, either way, I can test it at home (personally, I prefer the gem
statements to magic, moving-target require statements) but it doesn't
seem like your problem has anything to do with DBI or DBI::DBD::Mysql,
but either rubygems or your misunderstanding of the version you're
working with.

-Erik
--
Posted via http://www.ruby-....

Owein Herrmann

10/15/2008 7:41:00 PM

0

Erik Hollensbe wrote:
> Is this 1.9? I'm not sure how rubygems would be automatically required
> in 1.8.x. I guess it's also possible that the require overrides have
> been removed from rubygems. require_gem has been deprecated for some
> time and probably has been removed by now.
>
> Well, either way, I can test it at home (personally, I prefer the gem
> statements to magic, moving-target require statements) but it doesn't
> seem like your problem has anything to do with DBI or DBI::DBD::Mysql,
> but either rubygems or your misunderstanding of the version you're
> working with.
>
> -Erik

No, I'm 1.8.6... some patch level. I have been doing a lot of testing in
irb and have not really tried it in a program... maybe irb does some
automatic gem stuff that does not get done when running a program?
Anyway, I'll try it out on my ruby box when I'm done with work tonight
to see if that works differently than running stuff in irb.
--
Posted via http://www.ruby-....

Owein Herrmann

10/16/2008 1:05:00 AM

0



$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

$ gem env
RubyGems Environment:
- VERSION: 0.9.4 (0.9.4)
- INSTALLATION DIRECTORY: c:/ruby/lib/ruby/gems/1.8
- GEM PATH:
- c:/ruby/lib/ruby/gems/1.8
- REMOTE SOURCES:
- http://gems.rub...

start of my ruby program:
p require('rubygems')
p gem('mysql')
p gem('dbi')
dbh = DBI.connect('DBI:Mysql:money', 'mysqluser', 'xxxxxxx')

returns:
false
true
true
C:/ruby/code/rdbm.rb:9: uninitialized constant DBI (NameError)

so, when I start ruby, rubygems is already included? Interesting.

adding "require('irb') makes everything work. So my real problem is that
this is not how it says to do it in the 'book', and so I am concerned
that something might not be set up right.
--
Posted via http://www.ruby-....

Gregory Brown

10/16/2008 4:20:00 PM

0

On Wed, Oct 15, 2008 at 1:05 PM, Erik Hollensbe <erik@hollensbe.org> wrote:
> Owein Herrmann wrote:
>>> I don't know if this is recommended usage anymore, but provided you
>>> haven't installed dbi with the provided setup.rb, "require 'dbi'" should
>>> work without the gem statements.
>> Hmmmm... that definitely does not work. When I say "require 'dbi'" it
>> returns true, but then dbh = DBI.connect('DBI:Mysql:money', 'mysqluser',
>> 'pass3') causes an exception about DBI not being defined... I'm not on
>> my ruby box right now, so I cannot give the exact notation... basically,
>> what I show above works, the two gem commands and then the require.
>
> Is this 1.9? I'm not sure how rubygems would be automatically required
> in 1.8.x.

Via RUBY_OPT

--
Technical Blaag at: http://blog.majesticseacr... | Non-tech
stuff at: http://metametta.bl...

Erik Hollensbe

10/16/2008 5:18:00 PM

0

Owein Herrmann wrote:

> so, when I start ruby, rubygems is already included? Interesting.
>
> adding "require('irb') makes everything work. So my real problem is that
> this is not how it says to do it in the 'book', and so I am concerned
> that something might not be set up right.

Nah, the book is just old. :) I'm fairly certain I never built an
autorequire into the DBI gems, which is at least part of the problem.
I'm not sure changing it would be a solution, however.

-Erik
--
Posted via http://www.ruby-....

Erik Hollensbe

10/17/2008 8:38:00 AM

0

Owein Herrmann wrote:
> Quick question:
>
> I thought using rubygems that to use the dbi and mysql to access a MySQL
> database from Ruby I would only have to do the following:
>
> gem 'mysql'
> gem 'dbi'
>
> and then I could do whatever, eg:

I just wanted to address this because... it's been driving me nuts all
day. :)

Here's a script that works great on my dev system, stock libs, gem env,
DBI and DBD::Mysql latest release gems:

~% gem env
[1:19]
RubyGems Environment:
- VERSION: 0.9.4 (0.9.4)
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- GEM PATH:
- /var/lib/gems/1.8
- REMOTE SOURCES:
- http://gems.rub...

---

require "rubygems"
require "dbi"

begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:rubytest:localhost", "root")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end

---

If this breaks on your system, I'd be looking elsewhere (of ruby,
rubygems, and dbi... likely in the direction of your package
maintainers) to solve your problem.

-Erik
--
Posted via http://www.ruby-....

Owein Herrmann

10/18/2008 12:43:00 AM

0

Erik Hollensbe wrote:

> require "rubygems"
> require "dbi"
What about the mysql package? Don't you have to do something to load
that?

> dbh = DBI.connect("DBI:Mysql:rubytest:localhost", "root")
Using the lines above, this is where is breaks. It cannot locate the
mysql driver. If I add "require 'mysql'", then I get:
DBI::InterfaceError: Could not load driver (uninitialized constant
Mysql::Driver)
from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:344:in `load_driver'
from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:227:in
`_get_full_driver'
from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:213:in `connect'

The only correct way I have to initialize these packages for use seems
to be using the "gem" command, and then I have to require "dbi". Oh
well. At least it works.

> If this breaks on your system, I'd be looking elsewhere (of ruby,
> rubygems, and dbi... likely in the direction of your package
> maintainers) to solve your problem.

I'm not sure what you mean by package maintainers...
--
Posted via http://www.ruby-....

Grehom

1/10/2009 10:24:00 AM

0

Owein Herrmann wrote:
> Erik Hollensbe wrote:
>
>> require "rubygems"
>> require "dbi"
> What about the mysql package? Don't you have to do something to load
> that?
>
>> dbh = DBI.connect("DBI:Mysql:rubytest:localhost", "root")
> Using the lines above, this is where is breaks. It cannot locate the
> mysql driver. If I add "require 'mysql'", then I get:
> DBI::InterfaceError: Could not load driver (uninitialized constant
> Mysql::Driver)
> from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:344:in `load_driver'
> from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:227:in
> `_get_full_driver'
> from c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:213:in `connect'
>
> The only correct way I have to initialize these packages for use seems
> to be using the "gem" command, and then I have to require "dbi". Oh
> well. At least it works.

>> If this breaks on your system, I'd be looking elsewhere (of ruby,
>> rubygems, and dbi... likely in the direction of your package
>> maintainers) to solve your problem.
>
> I'm not sure what you mean by package maintainers...

Did this ever get solved? I have just built a new setup and wanted to
try ruby rails on it. It's Centos 5.2

gem env

RubyGems Environment:
- RUBYGEMS VERSION: 1.3.1
- RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- /home/grehom/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.ruby...


mysql is working fine

[grehom@localhost mysql]$ mysql --version
mysql Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using
readline 5.0

I get the same error running Erik's script:

[grehom@localhost mysql]$ ruby erics_test.rb
/usr/local/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi.rb:294:in
`load_driver': Unable to load driver 'Mysql' (underlying error:
uninitialized constant DBI::DBD::Mysql) (DBI::InterfaceError)
from /usr/local/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from
/usr/local/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi.rb:236:in
`load_driver'
from
/usr/local/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi.rb:154:in
`_get_full_driver'
from
/usr/local/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi.rb:139:in `connect'
from erics_test.rb:6
[grehom@localhost mysql]$

--
Posted via http://www.ruby-....