Method: SQLite3::Database#execute

Defined in:
lib/sqlite3/database.rb

#execute(sql, bind_vars = [], *args, &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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sqlite3/database.rb', line 208

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

    warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling `SQLite3::Database#execute` with nil or multiple bind params without using an array.  Please switch to passing bind parameters as an array. Support for bind parameters as *args will be removed in 2.0.0.
    eowarn
  end

  prepare( sql ) do |stmt|
    stmt.bind_params(bind_vars)
    stmt    = ResultSet.new self, stmt

    if block_given?
      stmt.each do |row|
        yield row
      end
    else
      stmt.to_a
    end
  end
end