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

栏目: Ruby · 发布时间: 5年前

内容简介:ActiveRecord::Base.connection.exec_query  用这个。  不要用 ...execute.例如:irb(main):006:0> a = ActiveRecord::Base.connection.exec_query "show status where `variable_name` = 'Threads_connected'";
参考: 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"

以上所述就是小编给大家介绍的《rails - 直接执行raw sql. 查询mysql的基本数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Elements of Programming

Elements of Programming

Alexander A. Stepanov、Paul McJones / Addison-Wesley Professional / 2009-6-19 / USD 39.99

Elements of Programming provides a different understanding of programming than is presented elsewhere. Its major premise is that practical programming, like other areas of science and engineering, mus......一起来看看 《Elements of Programming》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具