Skip to content


Repository files navigation

Ruby Interface for SQLite3


This library allows Ruby programs to use the SQLite3 database engine (

Note that this module is only compatible with SQLite 3.6.16 or newer.

Test suite

Quick start

For help understanding the SQLite3 Ruby API, please read the FAQ and the full API documentation.

A few key classes whose APIs are often-used are:

  • SQLite3::Database (rdoc)
  • SQLite3::Statement (rdoc)
  • SQLite3::ResultSet (rdoc)

If you have any questions that you feel should be addressed in the FAQ, please send them to the mailing list or open a discussion thread.

require "sqlite3"

# Open a database
db = "test.db"

# Create a table
rows = db.execute <<-SQL
  create table numbers (
    name varchar(30),
    val int

# Execute a few inserts
  "one" => 1,
  "two" => 2,
}.each do |pair|
  db.execute "insert into numbers values ( ?, ? )", pair

# Find a few rows
db.execute( "select * from numbers" ) do |row|
  p row
# => ["one", 1]
#    ["two", 2]

# Create another table with multiple columns
db.execute <<-SQL
  create table students (
    name varchar(50),
    email varchar(50),
    grade varchar(5),
    blog varchar(50)

# Execute inserts with parameter markers
db.execute("INSERT INTO students (name, email, grade, blog)
            VALUES (?, ?, ?, ?)", ["Jane", "", "A", ""])

db.execute( "select * from students" ) do |row|
  p row
# => ["Jane", "", "A", ""]

Thread Safety

When SQLite3.threadsafe? returns true, then SQLite3 has been compiled to support running in a multithreaded environment. However, this doesn't mean that all classes in the SQLite3 gem can be considered "thread safe".

When SQLite3.threadsafe? returns true, it is safe to share only SQLite3::Database instances among threads without providing your own locking mechanism. For example, the following code is fine because only the database instance is shared among threads:

require 'sqlite3'

db = ":memory:"

latch =

ts = { {
    db.execute "SELECT '#{Thread.current.inspect}'"
10.times { latch << nil }


Other instances can be shared among threads, but they require that you provide your own locking for thread safety. For example, SQLite3::Statement objects (prepared statements) are mutable, so applications must take care to add appropriate locks to avoid data race conditions when sharing these objects among threads.

Lets rewrite the above example but use a prepared statement and safely share the prepared statement among threads:

db = ":memory:"

# Prepare a statement
stmt = db.prepare "SELECT :inspect"
stmt_lock =

latch =

ts = { {

    # Add a lock when using the prepared statement.
    # Binding values, and walking over results will mutate the statement, so
    # in order to prevent other threads from "seeing" this thread's data, we
    # must lock when using the statement object
    stmt_lock.synchronize do

10.times { latch << nil }



It is generally recommended that if applications want to share a database among threads, they only share the database instance object. Other objects are fine to share, but may require manual locking for thread safety.


Installation or database extensions

If you're having trouble with installation, please first read

General help requests

You can ask for help or support:

Bug reports

You can file the bug at the github issues page.




This library is licensed under BSD-3-Clause, see LICENSE.


The source code of sqlite is distributed in the "ruby platform" gem. This code is public domain, see for details.