Method: SQLite3::Database#execute2

Defined in:
lib/sqlite3/database.rb

#execute2(sql, *bind_vars) ⇒ Object

Executes the given SQL statement, exactly as with #execute. However, the first row returned (either via the block, or in the returned array) is always the names of the columns. Subsequent rows correspond to the data from the result set.

Thus, even if the query itself returns no rows, this method will always return at least one row–the names of the columns.

See also #execute, #query, and #execute_batch for additional ways of executing statements.



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/sqlite3/database.rb', line 273

def execute2(sql, *bind_vars)
  prepare(sql) do |stmt|
    result = stmt.execute(*bind_vars)
    if block_given?
      yield stmt.columns
      result.each { |row| yield row }
    else
      return result.each_with_object([stmt.columns]) { |row, arr|
               arr << row
             }
    end
  end
end