0

I'm trying to make work JRuby and SQLite3 with the following gems:

bouncy-castle-java (1.5.0146.1)

bundler (1.3.2)

dbd-jdbc (0.1.6 java)

dbi (0.4.5)

deprecated (2.0.1)

jdbc-sqlite3 (3.7.2.1)

jruby-launcher (1.0.15 java)

jruby-openssl (0.8.2)

json (1.7.7 java)

rack (1.5.2)

rack-protection (1.4.0)

rake (10.0.3)

rubygems-bundler (1.1.1)

rvm (1.11.3.6)

sinatra (1.3.5)

sqlite3 (1.3.7)

tilt (1.3.5)

and this code:

require 'java'
require 'dbi'
require 'dbd/Jdbc'
require 'jdbc/sqlite3'

dbh = DBI.connect(
  "DBI:jdbc:sqlite:db.sqlite",  # connection string
   '',                                 # no username for sqlite3
   '',                                 # no password for sqlite3
   'driver' => 'org.sqlite.JDBC')      # need to set the driver

But I have this error:

DBI::InterfaceError: Unable to load driver 'jdbc' (underlying error: wrong constant name jdbc) load_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:300 mon_synchronize at /home/gl/.rvm/rubies/jruby-1.7.3/lib/ruby/1.9/monitor.rb:211 load_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:242 _get_full_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:160 connect at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:145 (root) at srv.rb:6

Have you got an idea?

3 Answers 3

1

I've just been trying to work this out myself, and eventually succeeded - the problem is that the driver name is now 'Jdbc', rather than 'jdbc' - capitalization is important, both in the connection string, and the require statement. If you change your connection string to "DBI:Jdbc:sqlite:db.sqlite" it should work fine.

0

I suggest you use ActiveRecord rather than DBI.

You can execute bare SQL thus:

ActiveRecord::Base.connection.execute("SELECT * FROM some_table") 
3
  • I want to put raw sql queries.
    – GlinesMome
    Apr 2, 2013 at 10:59
  • This doesn't answer the question - there are many reasons why someone might choose DBI over ActiveRecord, and plenty of situation in which a direct database access, rather than a heavyweight ORM abstraction would be a prudent choice.
    – mistertim
    Apr 2, 2013 at 14:00
  • The question was "have you got an idea?" So I put forward an idea that's worked for me in similar cases. Other constraints were not articulated. Apr 3, 2013 at 7:36
0

The accepted answer does not work (for me at least). I searched SO and the Web for hours (there's a lot of good and bad information out there) and finally came upon a solution that works (for me at least). A large part of the problem is solved by adding the line 'Jdbc::SQLite3.load_driver'.

require 'java'
require 'jdbc/sqlite3'

module JavaSql
  include_package 'java.sql'
end

Jdbc::SQLite3.load_driver
Java::org.sqlite.JDBC

conn_str = 'jdbc:sqlite:../Data/AflBettingHistory.db3'
conn = JavaSql::DriverManager.getConnection(conn_str)
stm = conn.createStatement
rs = stm.executeQuery("select Name from Team")
while (rs.next) do
  puts rs.getString("Name")
end
rs.close
stm.close
conn.close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.