Method: SQLite3::Database#execute
- Defined in:
- lib/sqlite3/database.rb
#execute(sql, bind_vars = [], *args, &block) ⇒ Object Also known as: exec
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.
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/sqlite3/database.rb', line 486 def execute sql, bind_vars = [], *args, &block if bind_vars.nil? || !args.empty? if args.empty? bind_vars = [] else bind_vars = [bind_vars] + args end end prepare( sql ) do |stmt| stmt.bind_params(bind_vars) columns = stmt.columns if block_given? stmt.each do |row| if @results_as_hash yield ordered_map_for(columns, row) else yield row end end else if @results_as_hash stmt.map { |row| ordered_map_for(columns, row) } else stmt.to_a end end end end |