Rasmus Debitsch wrote:
I want to access a MSAccess database with Ruby. I’m new to databases and
Ruby, so my question is quite basic.
When I try to connect to the database with DBI a error message shows up. I
have no idea if there is something wrong with my code or my system
configuration. Perhaps someone can provide a working code fragment?
Environment:
Windows ME, MSAccess not installed
Ruby 173-7
A mbd file
Code:
require ‘dbi’
dbq = “dbq=C:/Benutzer/Test.mdb”
dsn = “driver=Microsoft Access Driver(*.mdb);#{dbq}”
DBI.connect(“DBI:ODBC:#{dsn}”,“”,“”)
Error:
C:/PROGRAMME/RUBY/lib/ruby/site_ruby/1.7/DBD/ODBC/ODBC.rb:71:in connect': S1090 (0) [Microsoft][ODBC Driver Manager] Ungültige Zeichenfolgen- oder Pufferlänge. (DBI::DatabaseError) from C:/PROGRAMME/RUBY/lib/ruby/site_ruby/1.7/dbi/dbi.rb:496:in
connect’
from C:/PROGRAMME/RUBY/lib/ruby/site_ruby/1.7/dbi/dbi.rb:322:in `connect’
Rasmus
You can use the rubyinstaller @ rubyinstaller.sf.net (version 1.6.8) is
good. It contains the ODBC DBD/DBI drivers. Then all you need is to
install the MDAC package from microsoft (although you might already have
and ODBC driver). On Windows 98 go into the control panel and look for
ODBC datasources. There should be a tab that says ‘drivers’. Check for
a MS Access driver. You can then select the System tab, hit new, choose
MS Access, then select the MDB file you have on your drive. Next create
a test script to use the DBI:ODBC. Here’s some code I swiped myself:
require ‘dbi’
begin
# connect to the MySQL server
dbh = DBI.connect(“DBI:ODBC:lli”, “root”, “”)
# get server version string and display it
row = dbh.select_one(“SELECT VERSION()”)
puts "Server version: " + row[0]
dbh.select_all(“select * from types”) do |row|
puts row[0].to_s + " " + row[1]
end
rescue DBI::DatabaseError => e
puts “An error occurred”
print "Error code: ", e.err, “\n”
print "Error message: ", e.errstr, “\n”
ensure
# disconnect from server
dbh.disconnect if dbh
end
Replace ‘lli’ with whatever you named your ODBC datasource. Replace
‘root’ with a userid (with MS Access you probably won’t need one).
You can find more info on the ruby DBI @ http://ruby-dbi.sourceforge.net
Good skill to you!
Greg Brondo