Method: SQLite::Database#execute
- Defined in:
- lib/sqlite/database.rb
#execute(sql, *bind_vars) ⇒ Object
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.
Each placeholder must match one of the following formats:
-
?
-
?nnn
-
:word
-
:word:
where nnn is an integer value indicating the index of the bind variable to be bound at that position, and word is an alphanumeric identifier for that placeholder. For “?
”, an index is automatically assigned of one greater than the previous index used (or 1, if it is the first).
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.
The block is optional. If given, it will be invoked for each row returned by the query. Otherwise, any results are accumulated into an array and returned wholesale.
See also #execute2, #execute_batch and #query for additional ways of executing statements.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/sqlite/database.rb', line 191 def execute( sql, *bind_vars ) stmt = prepare( sql ) stmt.bind_params( *bind_vars ) result = stmt.execute begin if block_given? result.each { |row| yield row } else return result.inject( [] ) { |arr,row| arr << row; arr } end ensure result.close end end |