class SQLite3::Database


- SQLean::Crypto # Rails 8.1+ accepts the name of a constant that responds to ‘to_path`
- <%= SQLean::UUID.to_path %> # or ruby code returning a path
- .sqlpkg/nalgeon/crypto/crypto.so # a filesystem path
extensions:
adapter: sqlite3
development:
# config/database.yml
You can load extensions in a Rails application by using the extensions: configuration option:
subsequently invocations of #load_extension on the initialized Database object.
#enable_load_extension; however it is still necessary to call #enable_load_extensions before any
Note that when loading extensions via the constructor, there is no need to call
db = SQLite3::Database.new(“:memory:”, extensions: [“/path/to/extension”, SQLean::Crypto])
the extensions: keyword argument to pass an array of String paths or extension specifiers:
It’s also possible in v2.4.0+ to load extensions via the SQLite3::Database constructor by using
db.load_extension(SQLean::Crypto)
db.enable_load_extension(true)
db = SQLite3::Database.new(“:memory:”)
which provides modules that implement this interface, you can pass the module directly:
So, for example, if you are using the {sqlean gem}[https://github.com/flavorjones/sqlean-ruby]

end
def to_path: () → String
interface _ExtensionSpecifier
expressed in RBS syntax as:
documentation will refer to the supported interface as _ExtensionSpecifier, which can be
As of v2.4.0, it’s also possible to pass an object that responds to #to_path. This
db.load_extension(“/path/to/extension”)
db.enable_load_extension(true)
db = SQLite3::Database.new(“:memory:”)
existing Database object using the #load_extension method and passing a filesystem path:
extensions}[www.sqlite.org/loadext.html]. It’s possible to load an extension into an
SQLite3::Database supports the universe of {sqlite
== SQLite Extensions
more information.
to provide their own locks if they are to be shared among threads. Please see the README.md for
among threads without adding specific locking. Other object instances may require applications
When SQLite3.threadsafe? returns true, it is safe to share instances of the database class
== Thread safety
enabled results as hashes, then the results will all be indexible by field name.
Ara Howard. If you require the ArrayFields module before performing a query, and if you have not
Furthermore, the Database class has been designed to work well with the ArrayFields module from
the database–insertions and updates are all still typeless.
defined in the schemas for their tables). This translation only occurs when querying data from
(which are all represented as strings) may be converted into their corresponding types (as
The Database class provides type translation services as well, by which the SQLite3 data types
module for access to various pragma convenience methods.
It wraps the lower-level methods provided by the selected driver, and includes the Pragmas
end
end
p row
db.execute( “select * from table” ) do |row|
SQLite3::Database.new( “data.db” ) do |db|
require ‘sqlite3’
straightforward example of usage:
The Database class encapsulates a single connection to a SQLite3 database. Here’s a
== Overview

def self.arity

def self.arity
  @arity
end

def self.arity

def self.arity
  # this is what sqlite3_obj_method_arity did before
  @template.method(:step).arity
end

def self.finalize(&block)

def self.finalize(&block)
  define_method(:finalize_with_ctx, &block)
end

def self.name

def self.name
  @name
end

def self.name

def self.name
  @name
end

def self.step(&block)

def self.step(&block)
  define_method(:step_with_ctx, &block)
end

def self.template

def self.template
  @template
end

def authorizer(&block)

occur, and returning 2 causes the access to be silently denied.
is allowed to proceed. Returning 1 causes an authorization error to
to the database. If the block returns 0 (or +nil+), the statement
Installs (or removes) a block that will be invoked for every access
def authorizer(&block)
  self.authorizer = block
end

def build_result_set stmt

:nodoc:
This is not intended for general consumption
Given a statement, return a result set.
def build_result_set stmt
  if results_as_hash
    HashResultSet.new(self, stmt)
  else
    ResultSet.new(self, stmt)
  end
end

def busy_handler_timeout=(milliseconds)

while SQLite sleeps and retries.
This is an alternative to #busy_timeout, which holds the GVL
but only retries up to the indicated number of +milliseconds+.
Sets a #busy_handler that releases the GVL between retries,
def busy_handler_timeout=(milliseconds)
  timeout_seconds = milliseconds.fdiv(1000)
  busy_handler do |count|
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    if count.zero?
      @timeout_deadline = now + timeout_seconds
    elsif now > @timeout_deadline
      next false
    else
      sleep(0.001)
    end
  end
end

def commit

abort? and rollback or commit.
to allow it to be used in idioms like
this will cause an error to be raised. This returns +true+, in order
Commits the current transaction. If there is no current transaction,
def commit
  execute "commit transaction"
  true
end

def create_aggregate(name, arity, step = nil, finalize = nil,

aggregate functions.
See also #create_aggregate_handler for a more object-oriented approach to

puts db.get_first_value( "select lengths(name) from table" )

end
end
func.result = func[ :total ] || 0
finalize do |func|

end
func[ :total ] += ( value ? value.length : 0 )
func[ :total ] ||= 0
step do |func, value|
db.create_aggregate( "lengths", 1 ) do

Example:

store the result of the function.
function invocation. It should invoke FunctionProxy#result= to
single parameter, the FunctionProxy instance representing the current
The +finalize+ parameter must be a +proc+ object that accepts only a

The +step+ callback will be invoked once for each row of the result set.
invocation), with any subsequent parameters (up to the function's arity).
parameter a FunctionProxy instance (representing the function
The +step+ parameter must be a proc object that accepts as its first

variable arity functions, use -1 for the arity.)
The new function will be added as +name+, with the given +arity+. (For

a query.)
is the "count" function, for determining the number of rows that match
instead of over just a single row. (A very common aggregate function
functions are functions that apply over every row in the result set,
Creates a new aggregate function for use in SQL statements. Aggregate
def create_aggregate(name, arity, step = nil, finalize = nil,
  text_rep = Constants::TextRep::ANY, &block)
  proxy = Class.new do
    def self.step(&block)
      define_method(:step_with_ctx, &block)
    end
    def self.finalize(&block)
      define_method(:finalize_with_ctx, &block)
    end
  end
  if block
    proxy.instance_eval(&block)
  else
    proxy.class_eval do
      define_method(:step_with_ctx, step)
      define_method(:finalize_with_ctx, finalize)
    end
  end
  proxy.class_eval do
    # class instance variables
    @name = name
    @arity = arity
    def self.name
      @name
    end
    def self.arity
      @arity
    end
    def initialize
      @ctx = FunctionProxy.new
    end
    def step(*args)
      step_with_ctx(@ctx, *args)
    end
    def finalize
      finalize_with_ctx(@ctx)
      @ctx.result
    end
  end
  define_aggregator2(proxy, name)
end

def create_aggregate_handler(handler)

puts db.get_first_value( "select lengths(name) from A" )
db.create_aggregate_handler( LengthsAggregateHandler )

end
end
ctx.result = @total
def finalize( ctx )

end
@total += ( name ? name.length : 0 )
def step( ctx, name )

end
@total = 0
def initialize

def self.name; 'lengths'; end
def self.arity; 1; end
class LengthsAggregateHandler

Example:

#create_aggregate.
same signature as the +finalize+ callback for
aggregate function's evaluation. It should implement the
+finalize+:: this is the method that will be called to finalize the
signature as the +step+ callback for #create_aggregate.
aggregate function's evaluation. It should implement the same
+step+:: this is the method that will be called for each step of the

above), must respond to the following messages:
The handler instance (the object returned by the +new+ message, described

the function.
instance of the object that will handle a specific invocation of
+new+:: this must be implemented by the handler. It should return a new
this message.
+name+:: this is the name of the function. The handler _must_ implement
the function will have an arity of -1.
message is optional, and if the handler does not respond to it,
+arity+:: corresponds to the +arity+ parameter of #create_aggregate. This

handler should respond to the following messages:
(the "handler") that knows how to obtain all of that information. The
callbacks, arity, and type, you specify a factory object
#create_aggregate). Instead of explicitly specifying the name,
This is another approach to creating an aggregate function (see
def create_aggregate_handler(handler)
  # This is a compatibility shim so the (basically pointless) FunctionProxy
  # "ctx" object is passed as first argument to both step() and finalize().
  # Now its up to the library user whether he prefers to store his
  # temporaries as instance variables or fields in the FunctionProxy.
  # The library user still must set the result value with
  # FunctionProxy.result= as there is no backwards compatible way to
  # change this.
  proxy = Class.new(handler) do
    def initialize
      super
      @fp = FunctionProxy.new
    end
    def step(*args)
      super(@fp, *args)
    end
    def finalize
      super(@fp)
      @fp.result
    end
  end
  define_aggregator2(proxy, proxy.name)
  self
end

def create_function name, arity, text_rep = Constants::TextRep::UTF8, &block

puts db.get_first_value( "select maim(name) from table" )

end
end
func.result = value.split(//).sort.join
else
func.result = nil
if value.nil?
db.create_function( "maim", 1 ) do |func, value|

Example:

indicate the return value that way.
the FunctionProxy#result= method on the +func+ parameter and
The block does not return a value directly. Instead, it will invoke

arguments it needs (up to its arity).
instance that wraps this function invocation--and any other
The block should accept at least one parameter--the FunctionProxy

-1 for the arity.)
+name+, with the given +arity+. (For variable arity functions, use
Creates a new function for use in SQL statements. It will be added as
def create_function name, arity, text_rep = Constants::TextRep::UTF8, &block
  define_function_with_flags(name, text_rep) do |*args|
    fp = FunctionProxy.new
    block.call(fp, *args)
    fp.result
  end
  self
end

def define_aggregator(name, aggregator)

The functions arity is the arity of the +step+ method.
already provide a suitable +clone+.
individual instances of the aggregate function. Regular ruby objects
+aggregator+ object will serve as template that is cloned to provide the
_API Change:_ +aggregator+ must also implement +clone+. The provided

return value for the aggregator function.
+step+ will be called with row information and +finalize+ must return the
object +aggregator+. +aggregator+ must respond to +step+ and +finalize+.
Define an aggregate function named +name+ using a object template
def define_aggregator(name, aggregator)
  # Previously, this has been implemented in C. Now this is just yet
  # another compatibility shim
  proxy = Class.new do
    @template = aggregator
    @name = name
    def self.template
      @template
    end
    def self.name
      @name
    end
    def self.arity
      # this is what sqlite3_obj_method_arity did before
      @template.method(:step).arity
    end
    def initialize
      @klass = self.class.template.clone
    end
    def step(*args)
      @klass.step(*args)
    end
    def finalize
      @klass.finalize
    end
  end
  define_aggregator2(proxy, name)
  self
end

def encoding

Fetch the encoding set on this database

call-seq: db.encoding
def encoding
  Encoding.find super
end

def execute sql, bind_vars = [], &block

executing statements.
See also #execute2, #query, and #execute_batch for additional ways of

returned wholesale.
by the query. Otherwise, any results are accumulated into an array and
The block is optional. If given, it will be invoked for each row returned

the name of the placeholder to bind the value to.
key/value pairs are each bound separately, with the key being used as
Note that if any of the values passed to this are hashes, then the

the query.
they are treated as bind variables, and are bound to the placeholders in
Executes the given SQL statement. If additional parameters are given,
def execute sql, bind_vars = [], &block
  prepare(sql) do |stmt|
    stmt.bind_params(bind_vars)
    stmt = build_result_set stmt
    if block
      stmt.each do |row|
        yield row
      end
    else
      stmt.to_a.freeze
    end
  end
end

def execute2(sql, *bind_vars)

executing statements.
See also #execute, #query, and #execute_batch for additional ways of

return at least one row--the names of the columns.
Thus, even if the query itself returns no rows, this method will always

from the result set.
always the names of the columns. Subsequent rows correspond to the data
first row returned (either via the block, or in the returned array) is
Executes the given SQL statement, exactly as with #execute. However, the
def execute2(sql, *bind_vars)
  prepare(sql) do |stmt|
    result = stmt.execute(*bind_vars)
    if block_given?
      yield stmt.columns
      result.each { |row| yield row }
    else
      return result.each_with_object([stmt.columns]) { |row, arr|
               arr << row
             }
    end
  end
end

def execute_batch(sql, bind_vars = [])

executing statements.
See also #execute_batch2 for additional ways of

This always returns the result of the last statement.

statement.
in turn. The same bind parameters, if given, will be applied to each
string, ignoring all subsequent statements. This will execute each one
means of executing queries will only execute the first statement in the
Executes all SQL statements in the given string. By contrast, the other
def execute_batch(sql, bind_vars = [])
  sql = sql.strip
  result = nil
  until sql.empty?
    prepare(sql) do |stmt|
      unless stmt.closed?
        # FIXME: this should probably use sqlite3's api for batch execution
        # This implementation requires stepping over the results.
        if bind_vars.length == stmt.bind_parameter_count
          stmt.bind_params(bind_vars)
        end
        result = stmt.step
      end
      sql = stmt.remainder.strip
    end
  end
  result
end

def execute_batch2(sql, &block)

executing statements.
See also #execute_batch for additional ways of

a block can be passed to parse the values accordingly.
Because all values except for 'NULL' are returned as strings,

If no query is made, an empty array will be returned.
If a query is made, all values will be returned as strings.

in turn. Bind parameters cannot be passed to #execute_batch2.
string, ignoring all subsequent statements. This will execute each one
means of executing queries will only execute the first statement in the
Executes all SQL statements in the given string. By contrast, the other
def execute_batch2(sql, &block)
  if block
    result = exec_batch(sql, @results_as_hash)
    result.map do |val|
      yield val
    end
  else
    exec_batch(sql, @results_as_hash)
  end
end

def filename db_name = "main"

temporary or in-memory.
to "main". Main return `nil` or an empty string if the database is
Returns the filename for the database named +db_name+. +db_name+ defaults
def filename db_name = "main"
  db_filename db_name
end

def finalize

def finalize
  finalize_with_ctx(@ctx)
  @ctx.result
end

def finalize

def finalize
  super(@fp)
  @fp.result
end

def finalize

def finalize
  @klass.finalize
end

def get_first_row(sql, *bind_vars)

See also #get_first_value.

discarding all others. It is otherwise identical to #execute.
A convenience method for obtaining the first row of a result set, and
def get_first_row(sql, *bind_vars)
  execute(sql, *bind_vars).first
end

def get_first_value(sql, *bind_vars)

See also #get_first_row.

identical to #execute.
result set, and discarding all other values and rows. It is otherwise
A convenience method for obtaining the first value of the first row of a
def get_first_value(sql, *bind_vars)
  query(sql, bind_vars) do |rs|
    if (row = rs.next)
      return @results_as_hash ? row[rs.columns[0]] : row[0]
    end
  end
  nil
end

def initialize file, options = {}, zvfs = nil


- +extensions:+ Array[String | _ExtensionSpecifier] SQLite extensions to load into the database. See Database@SQLite+Extensions for more information.
- +default_transaction_mode:+ one of +:deferred+ (default), +:immediate+, or +:exclusive+. If a mode is not specified in a call to #transaction, this will be the default transaction mode.
- +results_as_hash:+ +boolish+ (default false), return rows as hashes instead of arrays
- +strict:+ +boolish+ (default false), disallow the use of double-quoted string literals (see https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted)
Other supported +options+:

- +utf16:+ +boolish+ (default false), is the filename's encoding UTF-16 (only needed if the filename encoding is not UTF_16LE or BE)
Supported encoding +options+:

- +flags:+ set the mode to a combination of SQLite3::Constants::Open flags.
- +readwrite:+ boolean (default false), true to set the mode to +READWRITE+
- +readonly:+ boolean (default false), true to set the mode to +READONLY+
- the default mode is READWRITE | CREATE
Supported permissions +options+:

Create a new Database object that opens the given file.

SQLite3::Database.new(file, options = {})
call-seq:
def initialize file, options = {}, zvfs = nil
  mode = Constants::Open::READWRITE | Constants::Open::CREATE
  file = file.to_path if file.respond_to? :to_path
  if file.encoding == ::Encoding::UTF_16LE || file.encoding == ::Encoding::UTF_16BE || options[:utf16]
    open16 file
  else
    # The three primary flag values for sqlite3_open_v2 are:
    # SQLITE_OPEN_READONLY
    # SQLITE_OPEN_READWRITE
    # SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE -- always used for sqlite3_open and sqlite3_open16
    mode = Constants::Open::READONLY if options[:readonly]
    if options[:readwrite]
      raise "conflicting options: readonly and readwrite" if options[:readonly]
      mode = Constants::Open::READWRITE
    end
    if options[:flags]
      if options[:readonly] || options[:readwrite]
        raise "conflicting options: flags with readonly and/or readwrite"
      end
      mode = options[:flags]
    end
    open_v2 file.encode("utf-8"), mode, zvfs
    if options[:strict]
      disable_quirk_mode
    end
  end
  @tracefunc = nil
  @authorizer = nil
  @progress_handler = nil
  @collations = {}
  @functions = {}
  @results_as_hash = options[:results_as_hash]
  @readonly = mode & Constants::Open::READONLY != 0
  @default_transaction_mode = options[:default_transaction_mode] || :deferred
  initialize_extensions(options[:extensions])
  ForkSafety.track(self)
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

def initialize

def initialize
  @ctx = FunctionProxy.new
end

def initialize

def initialize
  super
  @fp = FunctionProxy.new
end

def initialize

def initialize
  @klass = self.class.template.clone
end

def initialize_extensions(extensions) # :nodoc:

:nodoc:
def initialize_extensions(extensions) # :nodoc:
  return if extensions.nil?
  raise TypeError, "extensions must be an Array" unless extensions.is_a?(Array)
  return if extensions.empty?
  begin
    enable_load_extension(true)
    extensions.each do |extension|
      load_extension(extension)
    end
  ensure
    enable_load_extension(false)
  end
end

def load_extension(extension_specifier)


db.load_extension(SQLean::VSV)

[Example] Using the {sqlean gem}[https://github.com/flavorjones/sqlean-ruby]:

db.load_extension("/path/to/my_extension.so")

[Example] Using a filesystem path:

return value of that method is used as the filesystem path to the sqlite extension file.
to the sqlite extension file. If an object that responds to #to_path, the
- +extension_specifier+: (String | +_ExtensionSpecifier+) If a String, it is the filesystem path
[Parameters]

See also: Database@SQLite+Extensions

#enable_load_extension prior to using this method.
Loads an SQLite extension library from the named file. Extension loading must be enabled using

load_extension(extension_specifier) -> self
call-seq:
def load_extension(extension_specifier)
  if extension_specifier.respond_to?(:to_path)
    extension_specifier = extension_specifier.to_path
  elsif !extension_specifier.is_a?(String)
    raise TypeError, "extension_specifier #{extension_specifier.inspect} is not a String or a valid extension specifier object"
  end
  load_extension_internal(extension_specifier)
end

def open(*args)

returns the result of the block instead of the database instance.
With block, like new closes the database at the end, but unlike new
Without block works exactly as new.
def open(*args)
  database = new(*args)
  if block_given?
    begin
      yield database
    ensure
      database.close
    end
  else
    database
  end
end

def prepare sql


The Statement can then be executed using Statement#execute.

execute the statement; it merely prepares the statement for execution.
Returns a Statement object representing the given SQL. This does not
def prepare sql
  stmt = SQLite3::Statement.new(self, sql)
  return stmt unless block_given?
  begin
    yield stmt
  ensure
    stmt.close unless stmt.closed?
  end
end

def query(sql, bind_vars = [])

terminates.
with a block, +close+ will be invoked implicitly when the block
returned, or you could have problems with locks on the table. If called
You must be sure to call +close+ on the ResultSet instance that is

result = db.prepare( "select * from foo where a=?" ).execute( 5 )
# is the same as
result = db.query( "select * from foo where a=?", [5])

parameters to it, and calling execute:
This is a convenience method for creating a statement, binding
def query(sql, bind_vars = [])
  result = prepare(sql).execute(bind_vars)
  if block_given?
    begin
      yield result
    ensure
      result.close
    end
  else
    result
  end
end

def quote(string)

single-quote characters. The modified string is returned.
It replaces all instances of the single-quote character with two
Quotes the given string, making it safe to use in an SQL statement.
def quote(string)
  string.gsub("'", "''")
end

def readonly?

A helper to check before performing any operation
Returns +true+ if the database has been open in readonly mode
def readonly?
  @readonly
end

def rollback

abort? and rollback or commit.
to allow it to be used in idioms like
this will cause an error to be raised. This returns +true+, in order
Rolls the current transaction back. If there is no current transaction,
def rollback
  execute "rollback transaction"
  true
end

def step(*args)

def step(*args)
  step_with_ctx(@ctx, *args)
end

def step(*args)

def step(*args)
  super(@fp, *args)
end

def step(*args)

def step(*args)
  @klass.step(*args)
end

def transaction(mode = nil)

#rollback.
transaction explicitly, either by calling #commit, or by calling
If a block is not given, it is the caller's responsibility to end the

explicitly or you'll get an error when the block terminates.
a block is given, #commit and #rollback should never be called
raises an exception, a rollback will be performed instead. Note that if
transaction is committed when the block terminates. If the block
If a block is given, the database instance is yielded to it, and the

passed to #initialize, is used.
If `nil` is specified, the default transaction mode, which was
:immediate, or :exclusive.
The +mode+ parameter may be either :deferred,

exception.
by SQLite, so attempting to nest a transaction will result in a runtime
Begins a new transaction. Note that nested transactions are not allowed
def transaction(mode = nil)
  mode = @default_transaction_mode if mode.nil?
  execute "begin #{mode} transaction"
  if block_given?
    abort = false
    begin
      yield self
    rescue
      abort = true
      raise
    ensure
      abort and rollback or commit
    end
  else
    true
  end
end