Class: Statement
Defined in: | ../coffee/api.coffee |
Overview
Represents an prepared statement.
Prepared statements allow you to have a template sql string, that you can execute multiple times with different parameters.
You can't instantiate this class directly, you have to use a Database object in order to create a statement.
Warning: When you close a database (using db.close()), all its statements are closed too and become unusable.
Instance Method Summary
-
#
(Boolean)
bind(values)
Bind values to the parameters, after having reseted the statement
SQL statements can have parameters, named '?', '?NNN', ':VVV', '@VVV', '$VVV', where NNN is a number and VVV a string.
- # (Boolean) step() Execute the statement, fetching the the next line of result, that can be retrieved with Statement.get() .
- # (Array<String,Number,Uint8Array,null>) get(params) Get one row of results of a statement.
- # (Array<String>) getColumnNames() Get the list of column names of a row of result of a statement.
- # (Object) getAsObject(params) Get one row of result as a javascript object, associating column names with their value in the current row.
- # (void) run(values) Shorthand for bind + step + reset Bind the values, execute the statement, ignoring the rows it returns, and resets it
- # (void) reset() Reset a statement, so that it's parameters can be bound to new values It also clears all previous bindings, freeing the memory used by bound parameters.
- # (void) freemem() Free the memory allocated during parameter binding
- # (Boolean) free() Free the memory used by the statement
Instance Method Details
#
(Boolean)
bind(values)
Bind values to the parameters, after having reseted the statement
SQL statements can have parameters, named '?', '?NNN', ':VVV', '@VVV', '$VVV', where NNN is a number and VVV a string. This function binds these parameters to the given values.
Warning: ':', '@', and '$' are included in the parameters names
Binding values to named parameters
var stmt = db.prepare("UPDATE test SET a=@newval WHERE id BETWEEN $mini AND $maxi");
stmt.bind({$mini:10, $maxi:20, '@newval':5});
- Create a statement that contains parameters like '$VVV', ':VVV', '@VVV'
- Call Statement.bind with an object as parameter
Binding values to parameters
var stmt = db.prepare("UPDATE test SET a=? WHERE id BETWEEN ? AND ?");
stmt.bind([5, 10, 20]);
- Create a statement that contains parameters like '?', '?NNN'
- Call Statement.bind with an array as parameter
Value types
Javascript type | SQLite type |
---|---|
number | REAL, INTEGER |
boolean | INTEGER |
string | TEXT |
Array, Uint8Array | BLOB |
null | NULL |
#
(Boolean)
step()
Execute the statement, fetching the the next line of result, that can be retrieved with Statement.get() .
#
(Array<String,Number,Uint8Array,null>)
get(params)
Get one row of results of a statement. If the first parameter is not provided, step must have been called before get.
var stmt = db.prepare("SELECT * FROM test");
while (stmt.step()) console.log(stmt.get());
#
(Array<String>)
getColumnNames()
Get the list of column names of a row of result of a statement.
var stmt = db.prepare("SELECT 5 AS nbr, x'616200' AS data, NULL AS nothing;");
stmt.step(); // Execute the statement
console.log(stmt.getColumnNames()); // Will print ['nbr','data','nothing']
#
(Object)
getAsObject(params)
Get one row of result as a javascript object, associating column names with their value in the current row.
var stmt = db.prepare("SELECT 5 AS nbr, x'616200' AS data, NULL AS nothing;");
stmt.step(); // Execute the statement
console.log(stmt.getAsObject()); // Will print {nbr:5, data: Uint8Array([1,2,3]), nothing:null}
#
(void)
run(values)
Shorthand for bind + step + reset Bind the values, execute the statement, ignoring the rows it returns, and resets it
#
(void)
reset()
Reset a statement, so that it's parameters can be bound to new values It also clears all previous bindings, freeing the memory used by bound parameters.
#
(void)
freemem()
Free the memory allocated during parameter binding
#
(Boolean)
free()
Free the memory used by the statement