Method: SQLite3::Database#execute

Defined in:
lib/sqlite3/database.rb

#execute(sql, bind_vars = [], &block) ⇒ 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.

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, #query, and #execute_batch for additional ways of executing statements.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/sqlite3/database.rb', line 248

def execute sql, bind_vars = [], &block
  prepare(sql) do |stmt|
    stmt.bind_params(bind_vars)
    stmt = build_result_set stmt

    if block
      stmt.each do |row|
        yield row
      end
    else
      stmt.to_a.freeze
    end
  end
end