rails - 直接执行raw sql. 查询mysql的基本数据

访问量: 40

参考: https://stackoverflow.com/questions/10836805/how-can-i-iterate-through-a-mysql-result-set

ActiveRecord::Base.connection.exec_query  用这个。  不要用 ...execute.  

例如:

irb(main):006:0> a = ActiveRecord::Base.connection.exec_query "show status where `variable_name` = 'Threads_connected'";
=> #<ActiveRecord::Result:0x00005573b13980a8 @columns=["Variable_name", "Value"], @rows=[["Threads_connected", "175"]], @hash_rows=nil, @column_types={}>

irb(main):007:0> a.columns
=> ["Variable_name", "Value"]
irb(main):008:0> a.rows
=> [["Threads_connected", "175"]]
irb(main):009:0> a.to_hash
=> [{"Variable_name"=>"Threads_connected", "Value"=>"175"}]
irb(main):011:0> a.to_hash.first["Value"]
=> "175"

订阅/RSS Feed

Subscribe

分类/category