The SQLite adapter works with both the 2.x and 3.x series of SQLite with the sqlite-ruby drivers (available both as gems and from rubyforge.org/projects/sqlite-ruby/).

Options:

  • :database - Path to the database file.

Namespace
Methods
C
D
E
L
N
R
S
T
V
Class Public methods
new(connection, logger, config)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 77
def initialize(connection, logger, config)
  super(connection, logger)
  @statements = StatementPool.new(@connection,
                                  config.fetch(:statement_limit) { 1000 })
  @config = config

  if config.fetch(:prepared_statements) { true }
    @visitor = Arel::Visitors::SQLite.new self
  else
    @visitor = BindSubstitution.new self
  end
end
Instance Public methods
change_column_null(table_name, column_name, null, default = nil)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 431
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/sqlite_adapter.rb, line 143
def clear_cache!
  @statements.clear
end
create_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 301
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/sqlite_adapter.rb, line 136
def disconnect!
  super
  clear_cache!
  @connection.close rescue nil
end
empty_insert_statement_value()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 461
def empty_insert_statement_value
  "VALUES(NULL)"
end
exec_delete(sql, name = 'SQL', binds = [])
Also aliased as: exec_update
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 267
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/sqlite_adapter.rb, line 241
def exec_query(sql, name = nil, binds = [])
  log(sql, name, binds) do

    # Don't cache statements without bind values
    if binds.empty?
      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 = [])
explain(arel, binds = [])

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

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 222
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/sqlite_adapter.rb, line 273
def last_inserted_id(result)
  @connection.last_insert_row_id
end
release_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 309
def release_savepoint
  execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
rename_table(name, new_name)

Renames a table.

Example:

rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 388
def rename_table(name, new_name)
  exec_query "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
end
requires_reloading?()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 125
def requires_reloading?
  true
end
rollback_to_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 305
def rollback_to_savepoint
  execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
select_rows(sql, name = nil)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 297
def select_rows(sql, name = nil)
  exec_query(sql, name).rows
end
supports_add_column?()

Returns true if SQLite version is ‘3.1.6’ or greater, false otherwise.

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 130
def supports_add_column?
  sqlite_version >= '3.1.6'
end
supports_ddl_transactions?()

Returns true if SQLite version is ‘2.0.0’ or greater, false otherwise.

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 95
def supports_ddl_transactions?
  sqlite_version >= '2.0.0'
end
supports_explain?()

Returns true.

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 121
def supports_explain?
  true
end
supports_index_sort_order?()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 157
def supports_index_sort_order?
  sqlite_version >= '3.3.0'
end
supports_savepoints?()

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

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 100
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/sqlite_adapter.rb, line 106
def supports_statement_cache?
  true
end
table_exists?(name)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 340
def table_exists?(name)
  name && tables('SCHEMA', 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/sqlite_adapter.rb, line 394
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/sqlite_adapter.rb, line 560
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/sqlite_adapter.rb, line 556
def sqlite_version
  @sqlite_version ||= SQLiteAdapter::Version.new(select_value('select sqlite_version(*)'))
end
table_structure(table_name)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 470
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/sqlite_adapter.rb, line 568
def translate_exception(exception, message)
  case exception.message
  when %rcolumn(s)? .* (is|are) not unique/
    RecordNotUnique.new(message, exception)
  else
    super
  end
end