DBI

H

Henrik Larsen

Hi everyone,

I have a problem connecting my application to a database. I think this
is a ruby problem though Im running ruby on rails, but let me explain.
The thing is that I want to execute queries upon several different
databases by using DBI/DBD and not activerecord. For this I have created
a simple ruby class with the responsibility of fetching different data.
This class looks something like the this:

require 'dbi'
class DataAccess

def self.methodA
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:mysql:localhost", "username", "password")
# get server version string and display it
row = dbh.select_one("SELECT ...")
rescue DBI::DatabaseError => error
puts "An error occurred"
ensure
# disconnect from server
dbh.disconnect if dbh
end

def self.methodB
#same as above, different database
...
end

end


When these methods are executed i get the following error:
Could not load driver (undefined local variable or method `e' for
DBI:Module)
...


Anyone have a sollution for this? I have tried following every guide
there is to connect ruby to a database (installation of dbi/dbd). Maybe
someone could explain what im doing wrong?
 
A

Aldric Giacomoni

Henrik said:
Hi everyone,

I have a problem connecting my application to a database. I think this
is a ruby problem though Im running ruby on rails, but let me explain.
The thing is that I want to execute queries upon several different
databases by using DBI/DBD and not activerecord. For this I have created
a simple ruby class with the responsibility of fetching different data.
This class looks something like the this:

require 'dbi'
class DataAccess

def self.methodA
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:mysql:localhost", "username", "password")
# get server version string and display it
row = dbh.select_one("SELECT ...")
rescue DBI::DatabaseError => error
puts "An error occurred"
ensure
# disconnect from server
dbh.disconnect if dbh
end

def self.methodB
#same as above, different database
...
end

end


When these methods are executed i get the following error:
Could not load driver (undefined local variable or method `e' for
DBI:Module)
..


Anyone have a sollution for this? I have tried following every guide
there is to connect ruby to a database (installation of dbi/dbd). Maybe
someone could explain what im doing wrong?
It sounds like the machine on which you are running the code doesn't
know how to connect to a mySQL database -- install the ODBC drivers for
mySQL ?

--Aldric
 
B

Brian Candler

Henrik said:
When these methods are executed i get the following error:
Could not load driver (undefined local variable or method `e' for
DBI:Module)
...

(1) Try it from the command line, using irb - firstly using native DBI
commands, and then using your wrapper class. Only when it's working
there think about porting it into Rails.

(2) If you can't get it to work, make the smallest standalone code you
can which reproduces the problem.

(3) Post this standalone code, and post the *full* unedited backtrace
you get when running it. The above error message isn't much use, as it
didn't say whether the error was in the DBI code itself, or your code.

Finally, beware that DBI has changed quite a bit between 0.2 and 0.4, so
if you are reading random pages on the web, beware that they may be out
of date.

HTH,

Brian.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top