Module sqlite3

Library module for sqlite3.

Copyright © 21 Jun 2008 by Tee Teoh

Version: 1.0.0

Behaviours: gen_server.

Authors: Tee Teoh.

Description

Library module for sqlite3

Data Types

column_constraint()

column_constraint() = non_null | primary_key | {primary_key, pk_constraints()} | unique | {default, sql_value()} | {raw, string()}

See column_constraints().

column_constraints()

column_constraints() = column_constraint() | [column_constraint()]

See table_info().

db()

db() = atom() | pid()

Functions which take databases accept either the name the database is registered under or the PID.

option()

option() = {file, DbFile::string()} | in_memory | temporary

pk_constraint()

pk_constraint() = autoincrement | desc | asc

See pk_constraints().

pk_constraints()

pk_constraints() = pk_constraint() | [pk_constraint()]

See column_constraint().

result()

result() = {ok, pid()} | ignore | {error, any()}

sql_non_query_result()

sql_non_query_result() = ok | sqlite_error() | {rowid, integer()}

The result returned by functions which call the database but don't return any records.

sql_result()

sql_result() = sql_non_query_result() | [{columns, [string()]} | {rows, [tuple()]} | sqlite_error()]

The result returned by functions which query the database. If there are errors, list of three tuples is returned: [{columns, ListOfColumnNames}, {rows, ListOfResults}, ErrorTuple]. If there are no errors, the list has two elements.

sql_type()

sql_type() = integer | text | double | blob | atom() | string()

Types of SQLite columns are represented by atoms 'integer', 'text', 'double', 'blob'. Other atoms and strings may also be used (e.g. "VARCHAR(20)", 'smallint', etc.) See http://www.sqlite.org/datatype3.html.

sql_value()

sql_value() = null | number() | iodata() | {blob, binary()}

Values accepted in SQL statements are atom 'null', numbers, strings (represented as iodata()) and blobs.

sqlite_error()

sqlite_error() = {error, integer(), string()} | {error, any()}

Errors reported by SQLite side are represented by 3-element tuples containing atom 'error', SQLite result code (http://www.sqlite.org/c3ref/c_abort.html, http://www.sqlite.org/c3ref/c_busy_recovery.html) and an English-language error message.

Errors occuring on the Erlang side are represented by 2-element tuples with first element 'error'.

table_constraint()

table_constraint() = {primary_key, [atom()]} | {unique, [atom()]} | {raw, string()}

table_constraints()

table_constraints() = table_constraint() | [table_constraint()]

Currently supported constraints for table_info() and sqlite3:create_table/4.

table_id()

table_id() = atom() | binary() | string()

table_info()

table_info() = [{atom(), sql_type()} | {atom(), sql_type(), column_constraints()}]

Describes the columns of an SQLite table: each tuple contains name, type and constraints (if any) of one column.

Function Index

add_columns/2 Add columns to table structure table structure is a list of {column name, column type} pairs.
add_columns/3 Add columns to table structure The table structure is a list of {column name, column type} pairs.
bind/3
bind_timeout/4
changes/1 Get affected rows.
changes/2
clear_bindings/2
clear_bindings_timeout/3
close/1 Closes the Db sqlite3 database.
close_timeout/2 Closes the Db sqlite3 database.
columns/2
columns_timeout/3
create_table/2 Creates the Tbl table using TblInfo as the table structure.
create_table/3 Creates the Tbl table in Db using Columns as the table structure.
create_table/4 Creates the Tbl table in Db using Columns as the table structure and Constraints as table constraints.
create_table_timeout/4 Creates the Tbl table in Db using Columns as the table structure.
create_table_timeout/5 Creates the Tbl table in Db using Columns as the table structure and Constraints as table constraints.
delete/2 Delete a row from Tbl table in Db database such that the Value matches the value in Column.
delete/3 Delete a row from Tbl table in Db database such that the Value matches the value in Column.
delete_timeout/4 Delete a row from Tbl table in Db database such that the Value matches the value in Column.
drop_table/1 Drop the table Tbl.
drop_table/2 Drop the table Tbl from Db database.
drop_table_timeout/3 Drop the table Tbl from Db database.
enable_load_extension/2
finalize/2
finalize_timeout/3
list_tables/0 Returns a list of tables.
list_tables/1 Returns a list of tables for Db.
list_tables_timeout/2 Returns a list of tables for Db.
next/2
next_timeout/3
open/1 Opens the sqlite3 database in file DbName.db in the working directory (creating this file if necessary).
open/2 Opens a sqlite3 database creating one if necessary.
prepare/2
prepare_timeout/3
read/2 Reads a row from Tbl table such that the Value matches the value in Column.
read/3 Reads a row from Tbl table in Db database such that the Value matches the value in Column.
read/4 Reads a row from Tbl table in Db database such that the Value matches the value in Column.
read_all/2 Reads all rows from Table in Db.
read_all/3 Reads Columns in all rows from Table in Db.
read_all_timeout/3 Reads all rows from Table in Db.
read_all_timeout/4 Reads Columns in all rows from Table in Db.
read_timeout/4 Reads a row from Tbl table in Db database such that the Value matches the value in Column.
read_timeout/5 Reads a row from Tbl table in Db database such that the Value matches the value in Column.
reset/2
reset_timeout/3
sql_exec/1 Executes the Sql statement directly.
sql_exec/2 Executes the Sql statement directly on the Db database.
sql_exec/3 Executes the Sql statement with parameters Params directly on the Db database.
sql_exec_script/2 Executes the Sql script (consisting of semicolon-separated statements) directly on the Db database.
sql_exec_script_timeout/3 Executes the Sql script (consisting of semicolon-separated statements) directly on the Db database.
sql_exec_timeout/3 Executes the Sql statement directly on the Db database.
sql_exec_timeout/4 Executes the Sql statement with parameters Params directly on the Db database.
start_link/1 Opens the sqlite3 database in file DbName.db in the working directory (creating this file if necessary).
start_link/2 Opens a sqlite3 database creating one if necessary.
stop/0 Closes the sqlite3 database.
table_info/1 Returns table schema for Tbl.
table_info/2 Returns table schema for Tbl in Db.
table_info_timeout/3 Returns table schema for Tbl in Db.
update/3 Updates rows into Tbl table such that the Value matches the value in Key with Data.
update/4 Updates rows into Tbl table in Db database such that the Value matches the value in Key with Data.
update_timeout/5 Updates rows into Tbl table in Db database such that the Value matches the value in Key with Data.
vacuum/0 Vacuum the default database.
vacuum/1 Vacuum the Db database.
vacuum_timeout/2 Vacuum the Db database.
value_to_sql/1 Converts an Erlang term to an SQL string.
value_to_sql_unsafe/1 Converts an Erlang term to an SQL string.
write/2 Write Data into Tbl table.
write/3 Write Data into Tbl table in Db database.
write_many/2 Write all records in Data into table Tbl.
write_many/3 Write all records in Data into table Tbl in database Db.
write_many_timeout/4 Write all records in Data into table Tbl in database Db.
write_timeout/4 Write Data into Tbl table in Db database.

Function Details

add_columns/2

add_columns(Tbl::table_id(), Columns::table_info()) -> sql_non_query_result()

Add columns to table structure table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

add_columns/3

add_columns(Db::db(), Tbl::table_id(), Columns::table_info()) -> sql_non_query_result()

Add columns to table structure The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

bind/3

bind(Db::db(), Ref::reference(), Params::sql_params()) -> sql_non_query_result()

bind_timeout/4

bind_timeout(Db::db(), Ref::reference(), Params::sql_params(), Timeout::timeout()) -> sql_non_query_result()

changes/1

changes(Db) -> any()

Get affected rows.

changes/2

changes(Db, Timeout) -> any()

clear_bindings/2

clear_bindings(Db::db(), Ref::reference()) -> sql_non_query_result()

clear_bindings_timeout/3

clear_bindings_timeout(Db::db(), Ref::reference(), Timeout::timeout()) -> sql_non_query_result()

close/1

close(Db::db()) -> ok

Closes the Db sqlite3 database.

close_timeout/2

close_timeout(Db::db(), Timeout::timeout()) -> ok

Closes the Db sqlite3 database.

columns/2

columns(Db::db(), Ref::reference()) -> sql_non_query_result()

columns_timeout/3

columns_timeout(Db::db(), Ref::reference(), Timeout::timeout()) -> sql_non_query_result()

create_table/2

create_table(Tbl::table_id(), Columns::table_info()) -> sql_non_query_result()

Creates the Tbl table using TblInfo as the table structure. The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

create_table/3

create_table(Db::db(), Tbl::table_id(), Columns::table_info()) -> sql_non_query_result()

Creates the Tbl table in Db using Columns as the table structure. The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

create_table/4

create_table(Db::db(), Tbl::table_id(), Columns::table_info(), Constraints::table_constraints()) -> sql_non_query_result()

Creates the Tbl table in Db using Columns as the table structure and Constraints as table constraints. The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

create_table_timeout/4

create_table_timeout(Db::db(), Tbl::table_id(), Columns::table_info(), Timeout::timeout()) -> sql_non_query_result()

Creates the Tbl table in Db using Columns as the table structure. The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

create_table_timeout/5

create_table_timeout(Db::db(), Tbl::table_id(), Columns::table_info(), Constraints::table_constraints(), Timeout::timeout()) -> sql_non_query_result()

Creates the Tbl table in Db using Columns as the table structure and Constraints as table constraints. The table structure is a list of {column name, column type} pairs. e.g. [{name, text}, {age, integer}]

Returns the result of the create table call.

delete/2

delete(Tbl::table_id(), Key::{column_id(), sql_value()}) -> sql_non_query_result()

Delete a row from Tbl table in Db database such that the Value matches the value in Column. Value must have the same type as determined from table_info/3.

delete/3

delete(Db::db(), Tbl::table_id(), Key::{column_id(), sql_value()}) -> sql_non_query_result()

Delete a row from Tbl table in Db database such that the Value matches the value in Column. Value must have the same type as determined from table_info/3.

delete_timeout/4

delete_timeout(Db::db(), Tbl::table_id(), Key::{column_id(), sql_value()}, Timeout::timeout()) -> sql_non_query_result()

Delete a row from Tbl table in Db database such that the Value matches the value in Column. Value must have the same type as determined from table_info/3.

drop_table/1

drop_table(Tbl::table_id()) -> sql_non_query_result()

Drop the table Tbl.

drop_table/2

drop_table(Db::db(), Tbl::table_id()) -> sql_non_query_result()

Drop the table Tbl from Db database.

drop_table_timeout/3

drop_table_timeout(Db::db(), Tbl::table_id(), Timeout::timeout()) -> sql_non_query_result()

Drop the table Tbl from Db database.

enable_load_extension/2

enable_load_extension(Db, Value) -> any()

finalize/2

finalize(Db::db(), Ref::reference()) -> sql_non_query_result()

finalize_timeout/3

finalize_timeout(Db::db(), Ref::reference(), Timeout::timeout()) -> sql_non_query_result()

list_tables/0

list_tables() -> [table_id()]

Returns a list of tables.

list_tables/1

list_tables(Db::db()) -> [table_id()]

Returns a list of tables for Db.

list_tables_timeout/2

list_tables_timeout(Db::db(), Timeout::timeout()) -> [table_id()]

Returns a list of tables for Db.

next/2

next(Db::db(), Ref::reference()) -> tuple() | done | sqlite_error()

next_timeout/3

next_timeout(Db::db(), Ref::reference(), Timeout::timeout()) -> tuple() | done | sqlite_error()

open/1

open(DbName::atom()) -> result()

Opens the sqlite3 database in file DbName.db in the working directory (creating this file if necessary).

open/2

open(DbName::atom(), Options::[option()]) -> {ok, Pid::pid()} | ignore | {error, Error}

Opens a sqlite3 database creating one if necessary. By default the database will be called DbName.db in the current path (unless Db is 'anonymous', see below). This can be changed by passing the option {file, DbFile :: string()}. DbFile must be the full path to the sqlite3 db file. Can be used to open multiple sqlite3 databases per node. Must be use in conjunction with stop/1, sql_exec/2, create_table/3, list_tables/1, table_info/2, write/3, read/3, delete/3 and drop_table/2. If the name is an atom other than 'anonymous', it's used for registering the gen_server and must be unique. If the name is 'anonymous', the process isn't registered.

prepare/2

prepare(Db::db(), SQL::iodata()) -> {ok, reference()} | sqlite_error()

prepare_timeout/3

prepare_timeout(Db::db(), SQL::iodata(), Timeout::timeout()) -> {ok, reference()} | sqlite_error()

read/2

read(Tbl::table_id(), Key::{column_id(), sql_value()}) -> sql_result()

Reads a row from Tbl table such that the Value matches the value in Column. Value must have the same type as determined from table_info/2.

read/3

read(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}) -> sql_result()

Reads a row from Tbl table in Db database such that the Value matches the value in Column. ColValue must have the same type as determined from table_info/3.

read/4

read(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}, Columns::[column_id()]) -> sql_result()

Reads a row from Tbl table in Db database such that the Value matches the value in Column. Value must have the same type as determined from table_info/3.

read_all/2

read_all(Db::db(), Tbl::table_id()) -> sql_result()

Reads all rows from Table in Db.

read_all/3

read_all(Db::db(), Tbl::table_id(), Columns::[column_id()]) -> sql_result()

Reads Columns in all rows from Table in Db.

read_all_timeout/3

read_all_timeout(Db::db(), Tbl::table_id(), Timeout::timeout()) -> sql_result()

Reads all rows from Table in Db.

read_all_timeout/4

read_all_timeout(Db::db(), Tbl::table_id(), Columns::[column_id()], Timeout::timeout()) -> sql_result()

Reads Columns in all rows from Table in Db.

read_timeout/4

read_timeout(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}, Timeout::timeout()) -> sql_result()

Reads a row from Tbl table in Db database such that the Value matches the value in Column. ColValue must have the same type as determined from table_info/3.

read_timeout/5

read_timeout(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}, Columns::[column_id()], Timeout::timeout()) -> sql_result()

Reads a row from Tbl table in Db database such that the Value matches the value in Column. Value must have the same type as determined from table_info/3.

reset/2

reset(Db::db(), Ref::reference()) -> sql_non_query_result()

reset_timeout/3

reset_timeout(Db::db(), Ref::reference(), Timeout::timeout()) -> sql_non_query_result()

sql_exec/1

sql_exec(SQL::iodata()) -> sql_result()

Executes the Sql statement directly.

sql_exec/2

sql_exec(Db::db(), SQL::iodata()) -> sql_result()

Executes the Sql statement directly on the Db database. Returns the result of the Sql call.

sql_exec/3

sql_exec(Db::db(), SQL::iodata(), Params::[sql_value() | {atom() | string() | integer(), sql_value()}]) -> sql_result()

Executes the Sql statement with parameters Params directly on the Db database. Returns the result of the Sql call.

sql_exec_script/2

sql_exec_script(Db::db(), SQL::iodata()) -> [sql_result()]

Executes the Sql script (consisting of semicolon-separated statements) directly on the Db database.

If an error happens while executing a statement, no further statements are executed.

The return value is the list of results of all executed statements.

sql_exec_script_timeout/3

sql_exec_script_timeout(Db::db(), SQL::iodata(), Timeout::timeout()) -> [sql_result()]

Executes the Sql script (consisting of semicolon-separated statements) directly on the Db database.

If an error happens while executing a statement, no further statements are executed.

The return value is the list of results of all executed statements.

sql_exec_timeout/3

sql_exec_timeout(Db::db(), SQL::iodata(), Timeout::timeout()) -> sql_result()

Executes the Sql statement directly on the Db database. Returns the result of the Sql call.

sql_exec_timeout/4

sql_exec_timeout(Db::db(), SQL::iodata(), Params::[sql_value() | {atom() | string() | integer(), sql_value()}], Timeout::timeout()) -> sql_result()

Executes the Sql statement with parameters Params directly on the Db database. Returns the result of the Sql call.

start_link/1

start_link(DbName::atom()) -> result()

Opens the sqlite3 database in file DbName.db in the working directory (creating this file if necessary). This is the same as open/1.

start_link/2

start_link(DbName::atom(), Options::[option()]) -> result()

Opens a sqlite3 database creating one if necessary. By default the database will be called DbName.db in the current path. This can be changed by passing the option {file, DbFile :: String()}. DbFile must be the full path to the sqlite3 db file. start_link/1 can be use with stop/0, sql_exec/1, create_table/2, list_tables/0, table_info/1, write/2, read/2, delete/2 and drop_table/1. This is the same as open/2.

stop/0

stop() -> ok

Closes the sqlite3 database.

table_info/1

table_info(Tbl::table_id()) -> table_info()

Returns table schema for Tbl.

table_info/2

table_info(Db::db(), Tbl::table_id()) -> table_info()

Returns table schema for Tbl in Db.

table_info_timeout/3

table_info_timeout(Db::db(), Tbl::table_id(), Timeout::timeout()) -> table_info()

Returns table schema for Tbl in Db.

update/3

update(Tbl::table_id(), X2::{column_id(), sql_value()}, Data::[{column_id(), sql_value()}]) -> sql_non_query_result()

Updates rows into Tbl table such that the Value matches the value in Key with Data.

update/4

update(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}, Data::[{column_id(), sql_value()}]) -> sql_non_query_result()

Updates rows into Tbl table in Db database such that the Value matches the value in Key with Data.

update_timeout/5

update_timeout(Db::db(), Tbl::table_id(), X3::{column_id(), sql_value()}, Data::[{column_id(), sql_value()}], Timeout::timeout()) -> sql_non_query_result()

Updates rows into Tbl table in Db database such that the Value matches the value in Key with Data.

vacuum/0

vacuum() -> sql_non_query_result()

Vacuum the default database.

vacuum/1

vacuum(Db::db()) -> sql_non_query_result()

Vacuum the Db database.

vacuum_timeout/2

vacuum_timeout(Db::db(), Timeout::timeout()) -> sql_non_query_result()

Vacuum the Db database.

value_to_sql/1

value_to_sql(X::sql_value()) -> iolist()

Converts an Erlang term to an SQL string. Currently supports integers, floats, 'null' atom, and iodata (binaries and iolists) which are treated as SQL strings.

All single quotes (') will be replaced with ''.

Reexported from sqlite3_lib:value_to_sql/1 for user convenience.

value_to_sql_unsafe/1

value_to_sql_unsafe(X::sql_value()) -> iolist()

Converts an Erlang term to an SQL string. Currently supports integers, floats, 'null' atom, and iodata (binaries and iolists) which are treated as SQL strings.

Note that it opens opportunity for injection if an iolist includes single quotes! Replace all single quotes (') with '' manually, or use value_to_sql/1 if you are not sure if your strings contain single quotes (e.g. can be entered by users).

Reexported from sqlite3_lib:value_to_sql/1 for user convenience.

write/2

write(Tbl::table_id(), Data::[{column_id(), sql_value()}]) -> sql_non_query_result()

Write Data into Tbl table. Value must be of the same type as determined from table_info/2.

write/3

write(Db::db(), Tbl::table_id(), Data::[{column_id(), sql_value()}]) -> sql_non_query_result()

Write Data into Tbl table in Db database. Value must be of the same type as determined from table_info/3.

write_many/2

write_many(Tbl::table_id(), Data::[[{column_id(), sql_value()}]]) -> [sql_result()]

Write all records in Data into table Tbl. Value must be of the same type as determined from table_info/2.

write_many/3

write_many(Db::db(), Tbl::table_id(), Data::[[{column_id(), sql_value()}]]) -> [sql_result()]

Write all records in Data into table Tbl in database Db. Value must be of the same type as determined from table_info/3.

write_many_timeout/4

write_many_timeout(Db::db(), Tbl::table_id(), Data::[[{column_id(), sql_value()}]], Timeout::timeout()) -> [sql_result()]

Write all records in Data into table Tbl in database Db. Value must be of the same type as determined from table_info/3.

write_timeout/4

write_timeout(Db::db(), Tbl::table_id(), Data::[{column_id(), sql_value()}], Timeout::timeout()) -> sql_non_query_result()

Write Data into Tbl table in Db database. Value must be of the same type as determined from table_info/3.


Generated by EDoc