The SQLite3 adapter works SQLite 3.6.16 or newer with the sqlite3-ruby drivers (available as gem from rubygems.org/gems/sqlite3).

Options:

  • :database- Path to the database file.

Namespace
Methods
A
C
D
E
L
N
Q
R
S
T
V
Class Public methods
new(connection, logger, config)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 107
def initialize(connection, logger, config)
  super(connection, logger)

  @active     = nil
  @statements = StatementPool.new(@connection,
                                  self.class.type_cast_config_to_integer(config.fetch(:statement_limit) { 1000 }))
  @config = config

  if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
    @prepared_statements = true
    @visitor = Arel::Visitors::SQLite.new self
  else
    @visitor = unprepared_visitor
  end
end
Instance Public methods
active?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 162
def active?
  @active != false
end
allowed_index_name_length()

Returns 62. SQLite supports index names up to 64 characters. The rest is used by rails internally to perform temporary rename operations

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 196
def allowed_index_name_length
  index_name_length - 2
end
change_column_null(table_name, column_name, null, default = nil)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 474
def change_column_null(table_name, column_name, null, default = nil)
  unless null || default.nil?
    exec_query("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
  end
  alter_table(table_name) do |definition|
    definition[column_name].null = null
  end
end
clear_cache!()

Clears the prepared statements cache.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 175
def clear_cache!
  @statements.clear
end
create_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 354
def create_savepoint
  execute("SAVEPOINT #{current_savepoint_name}")
end
disconnect!()

Disconnects from the database if already connected. Otherwise, this method does nothing.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 168
def disconnect!
  super
  @active = false
  @connection.close rescue nil
end
encoding()

Returns the current database encoding format as a string, eg: 'UTF-8'

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 218
def encoding
  @connection.encoding.to_s
end
exec_delete(sql, name = 'SQL', binds = [])
Also aliased as: exec_update
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 320
def exec_delete(sql, name = 'SQL', binds = [])
  exec_query(sql, name, binds)
  @connection.changes
end
exec_query(sql, name = nil, binds = [])
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 294
def exec_query(sql, name = nil, binds = [])
  log(sql, name, binds) do

    # Don't cache statements if they are not prepared
    if without_prepared_statement?(binds)
      stmt    = @connection.prepare(sql)
      cols    = stmt.columns
      records = stmt.to_a
      stmt.close
      stmt = records
    else
      cache = @statements[sql] ||= {
        :stmt => @connection.prepare(sql)
      }
      stmt = cache[:stmt]
      cols = cache[:cols] ||= stmt.columns
      stmt.reset!
      stmt.bind_params binds.map { |col, val|
        type_cast(val, col)
      }
    end

    ActiveRecord::Result.new(cols, stmt.to_a)
  end
end
exec_update(sql, name = 'SQL', binds = [])
Alias for: exec_delete
explain(arel, binds = [])

DATABASE STATEMENTS ======================================

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 275
def explain(arel, binds = [])
  sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
  ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
end
last_inserted_id(result)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 326
def last_inserted_id(result)
  @connection.last_insert_row_id
end
quote(value, column = nil)

QUOTING ==================================================

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 229
def quote(value, column = nil)
  if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
    s = column.class.string_to_binary(value).unpack("H*")[0]
    "x'#{s}'"
  else
    super
  end
end
quote_table_name_for_assignment(table, attr)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 242
def quote_table_name_for_assignment(table, attr)
  quote_column_name(attr)
end
release_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 362
def release_savepoint
  execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
rename_table(table_name, new_name)

Renames a table.

Example:

rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 441
def rename_table(table_name, new_name)
  exec_query "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
  rename_table_indexes(table_name, new_name)
end
requires_reloading?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 153
def requires_reloading?
  true
end
rollback_to_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 358
def rollback_to_savepoint
  execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
select_rows(sql, name = nil)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 350
def select_rows(sql, name = nil)
  exec_query(sql, name).rows
end
supports_add_column?()

Returns true

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 158
def supports_add_column?
  true
end
supports_ddl_transactions?()

Returns true

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 128
def supports_ddl_transactions?
  true
end
supports_explain?()

Returns true.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 223
def supports_explain?
  true
end
supports_index_sort_order?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 189
def supports_index_sort_order?
  true
end
supports_savepoints?()

Returns true if SQLite version is '3.6.8' or greater, false otherwise.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 133
def supports_savepoints?
  sqlite_version >= '3.6.8'
end
supports_statement_cache?()

Returns true, since this connection adapter supports prepared statement caching.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 139
def supports_statement_cache?
  true
end
table_exists?(table_name)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 393
def table_exists?(table_name)
  table_name && tables(nil, table_name).any?
end
valid_alter_table_options( type, options)

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 448
def valid_alter_table_options( type, options)
  type.to_sym != :primary_key
end
Instance Protected methods
default_primary_key_type()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 609
def default_primary_key_type
  if supports_autoincrement?
    'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
  else
    'INTEGER PRIMARY KEY NOT NULL'
  end
end
sqlite_version()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 605
def sqlite_version
  @sqlite_version ||= SQLite3Adapter::Version.new(select_value('select sqlite_version(*)'))
end
table_structure(table_name)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 510
def table_structure(table_name)
  structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA').to_hash
  raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
  structure
end
translate_exception(exception, message)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 617
def translate_exception(exception, message)
  case exception.message
  when /column(s)? .* (is|are) not unique/
    RecordNotUnique.new(message, exception)
  else
    super
  end
end