如何显示在服务器控制台SQL结果 [英] How to show sql result on server console
本文介绍了如何显示在服务器控制台SQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在的Rails V2.3 红宝石1.8 ,如果我跑的 sql语句在我的模型类以下code:
In Rails v2.3 , Ruby 1.8, if I run a sql statement with following code in my model class:
ActiveRecord::Base.connection.execute("select count(*) from cars;")
我怎样才能显示查询结果服务器控制台?
我想:
rslt = ActiveRecord::Base.connection.execute("select count(*) from cars;")
p rslt
但它只返回我MySQL的结果对象服务器控制台,而不是确切的结果上。
but it only returns me "MySQL result object" on the server console, not the exact result.
推荐答案
有几种方法可以从查询获得MySQL答案。你可以调用每一个,它会每行(只有一行 - 在你的案件数)迭代。看看 mysql的宝石文档看其他可用的方法,如each_hash和all_hashes。
There are couple ways to get mysql "answer" from your query. you can call each and it will iterate over each row (just one row - count in your case). take a look at mysql gem docs to see other available methods such as each_hash and all_hashes.
rslt = ActiveRecord::Base.connection.execute("select count(*) from cars;")
rslt.each {|mysql_result| puts mysql_result}
这篇关于如何显示在服务器控制台SQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文