Search code examples
rubywindowssqlitejrubysequel

SQLite3 with JRuby and Sequel


I need to use SQLite3 with Jruby and Sequel gem on Windows.

require 'sequel'
DB = Sequel.sqlite

I got this error:

Sequel::AdapterNotFound: LoadError: no such file to load -- sqlite3
     load_adapter at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/database/connecting.rb:93
    adapter_class at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/database/connecting.rb:17
          connect at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/database/connecting.rb:45
          connect at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/core.rb:116
   adapter_method at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/core.rb:394
  block in sqlite at C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sequel-5.9.0/lib/sequel/core.rb:401
           <main> at test.rb:53

When i try to install sqlite3 gem it fails when building native extensions:

Building native extensions. This could take a while...
ERROR:  Error installing sqlite3:
        ERROR: Failed to build gem native extension.

    current directory: C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/sqlite3-1.3.1
3/ext/sqlite3
C:/jruby-9.1.13.0/bin/jruby.exe -r ./siteconf20181005-6576-1e4h1cf.rb extconf.rb

checking for sqlite3.h... RuntimeError: The compiler failed to generate an executable file.
You have to install development tools first.

                 try_do at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:456
                try_cpp at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:587
   block in find_header at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:1144
  block in checking_for at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:942
      block in postpone at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:350
                   open at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:320
      block in postpone at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:350
                   open at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:320
               postpone at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:346
           checking_for at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:941
            find_header at C:/jruby-9.1.13.0/lib/ruby/stdlib/mkmf.rb:1143
                 <main> at extconf.rb:50
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/jruby-9.1.13.0/bin/jruby
        --with-sqlite3-config
        --without-sqlite3-config
        --with-pkg-config
        --without-pkg-config
        --with-sqlite3-dir
        --without-sqlite3-dir
        --with-sqlite3-include
        --without-sqlite3-include=${sqlite3-dir}/include
        --with-sqlite3-lib
        --without-sqlite3-lib=${sqlite3-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can
 be found here:

  C:/jruby-9.1.13.0/lib/ruby/gems/shared/extensions/universal-java-1.8/2.3.0/sql
ite3-1.3.13/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in C:/jruby-9.1.13.0/lib/ruby/gems/shared/gems/s
qlite3-1.3.13 for inspection.
Results logged to C:/jruby-9.1.13.0/lib/ruby/gems/shared/extensions/universal-ja
va-1.8/2.3.0/sqlite3-1.3.13/gem_make.out

I've copied SQLite lib (sqlite3.dll, sqlite3.def) in jruby/bin folder.

JRuby official page sugget use activerecord-jdbc-adapter, but I need to create in memory db, not connecting to existing one.

How can I do?

My environment:
Jruby v. 9.1.13
Sequel v. 5.9.0
Windows Server 2012 R2


Solution

  • You want to use the Sequel jdbc adapter and the jdbc-sqlite3 gem:

    require 'sequel'
    DB = Sequel.connect("jdbc:sqlite::memory:")
    

    FWIW, if you want an in-memory database with JRuby, h2, hsqldb, and derby may be better choices (and Sequel supports all of them).