without-brains.net

A software developer's blog

Making SQLite 3 work with JRuby and Rails

SQLite has the advantage that it is an embeddable database, so you don’t need a separate database server to use it (which is very convenient when developing software). Note that when you are using JRuby you can also use Apache Derby (which is an embeddable database written in Java) instead of SQLite if you want to.

When you use the standard version of Ruby getting SQLite support is easy, you just install the sqlite3 gem (gem install sqlite3) and you’re done (you need to have the SQLite libraries and dev headers installed on your system already of course). This does not work for JRuby because the sqlite3 is a native gem (containing C code with extensions on standard Ruby).

So how do you get SQLite to work with JRuby and Rails? Get the gems for JRuby! Assuming that you have SQLite installed on your system, you simply need to install the activerecord-jdbcsqlite3-adapter gem (this will automatically try to install the activerecord-jdbc-adapter and jdbc-sqlite3 gems if you don’t already have them). In your Rails project edit config/database.yml and change the adapter type to jdbcsqlite3 and you’re done :)

There are a number of activerecord-jdbc gems available (just type “gem search –remote activerecord-jdbc” in your shell to see them), including connectors for Apache Derby, MySQL and PostgreSQL.