0

我正在寻找一种方法来获取 mysql 查询的结果集,该查询类似于 ActiveRecord::Base.connection.select_rows 的输出,但也包括结果的列名称作为第一个元素。

例如:

ActiveRecord::Base.connection.select_rows_with_fields("SELECT id,name FROM users")
     => [["id", "name"], ["1","amy"],["2","bob"],["3","cam"]]
4

1 回答 1

1

select_rows_with_fields:

module ActiveRecord
  module ConnectionAdapters
    class Mysql2Adapter
      def select_rows_with_fields(sql)
        result = execute(sql)
        rows = result.to_a
        rows.unshift(result.fields)
      end
    end
  end
end
于 2013-10-06T17:11:24.407 回答