class SQLite3::Database

Overview

The Database class encapsulates single connection to an SQLite3 database. Its usage is very straightforward:

require "sqlite3"

db = SQLite3::Database.new( "data.db" )
db.execute("select * from table") do |row|
  p row
end
db.close

Lower level methods are also provided.

Superclass hierarchy

Object
Reference
SQLite3::Database

Class Method Summary

Instance Method Summary

Class Method Detail

def self.new(filename, &block)

Creates a new Database object that opens the given file, yields it, and closes it at the end.


[View source]

def self.new(filename)

Creates a new Database object that opens the given file.


[View source]

Instance Method Detail

def close

Closes this database.


[View source]

def closed?

Returns true if this database instance has been closed (see #close).


[View source]

def execute(sql, *binds, &block)

Executes the given SQL statement. If additional parameters are given, they are treated as bind variables, and are bound to the placeholders in the query.

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

Yields one Array(Value) for each result.


[View source]

def execute(sql, binds : Enumerable)

Executes the given SQL statement. If additional parameters are given, they are treated as bind variables, and are bound to the placeholders in the query.

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

Returns an Array(Array(Value)).


[View source]

def execute(sql, *binds)

Executes the given SQL statement. If additional parameters are given, they are treated as bind variables, and are bound to the placeholders in the query.

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

Returns an Array(Array(Value)).


[View source]

def execute(sql, binds : Enumerable, &block)

Executes the given SQL statement. If additional parameters are given, they are treated as bind variables, and are bound to the placeholders in the query.

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

Yields one Array(Value) for each result.


[View source]

def get_first_row(sql, binds : Enumerable)

A convenience method that returns the first row of a query result.


[View source]

def get_first_row(sql, *binds)

A convenience method that returns the first row of a query result.


[View source]

def get_first_value(sql, binds : Enumerable)

A convenience method that returns the first value of the first row of a query result.


[View source]

def get_first_value(sql, *binds)

A convenience method that returns the first value of the first row of a query result.


[View source]

def last_insert_row_id

Obtains the unique row ID of the last row to be inserted by this Database instance. This is an Int64.


[View source]

def prepare(sql)

Prepares an sql statement. Returns a Statement.


[View source]

def query(sql, binds : Enumerable)

Executes a query and gives back a ResultSet.


[View source]

def query(sql, *binds)

Executes a query and gives back a ResultSet.


[View source]

def query(sql, *binds, &block)

Executes a query and yields a ResultSet that will be closed at the end of the given block.


[View source]

def query(sql, binds : Enumerable, &block)

Executes a query and yields a ResultSet that will be closed at the end of the given block.


[View source]

def quote(string)

Quotes the given string, making it safe to use in an SQL statement. It replaces all instances of the single-quote character with two single-quote characters.


[View source]