Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

I suggest you use ActiveRecord rather than DBI.

You can execute bare SQL thus:

ActiveRecord::Base.connection.execute("SELECT * FROM some_table") 

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top