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.

Methods
C
D
E
L
N
R
S
T
V
Classes and Modules
Class Public methods
new(connection, logger, config)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 51
def initialize(connection, logger, config)
  super(connection, logger)
  @statements = {}
  @config = config
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 348
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 109
def clear_cache!
  @statements.values.each { |hash| hash[:stmt].close }
  @statements.clear
end
create_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 230
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 102
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 376
def empty_insert_statement_value
  "VALUES(NULL)"
end
exec_delete(sql, name = 'SQL', binds = [])
This method is also aliased as exec_update
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 196
def exec_delete(sql, name = 'SQL', binds = [])
  exec_query(sql, name, binds)
  @connection.changes
end
exec_query(sql, name = nil, binds = [])

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

# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 170
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 = [])
last_inserted_id(result)
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 202
def last_inserted_id(result)
  @connection.last_insert_row_id
end
release_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 238
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 312
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 91
def requires_reloading?
  true
end
rollback_to_savepoint()
# File activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb, line 234
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 226
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 96
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 66
def supports_ddl_transactions?
  sqlite_version >= '2.0.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 71
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 77
def supports_statement_cache?
  true
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 318
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 474
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 470
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 385
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 482
def translate_exception(exception, message)
  case exception.message
  when /column(s)? .* (is|are) not unique/
    RecordNotUnique.new(message, exception)
  else
    super
  end
end