读书人

ruby之怎么执行自己写的SQL

发布时间: 2012-03-01 10:25:47 作者: rapoo

ruby之如何执行自己写的SQL
我现在对一张表进行统计,必须执行自己手写的sql语句。比如: select author,count(name) as totalname from wiki where createdate >= '2008-10-01' and createdate <= '2008-10-30' group by author
不知道这样的功能在ruby中如何实现。
因为我是新手,希望能描述的越详细越好,最好能贴一下代码出来。不甚感激!!!
只要能实现此功能,分不够的可以再加!!!!!

[解决办法]
你用的是什么数据库,应该有很多种方法的,参考

http://www.troubleshooters.com/codecorn/ruby/database/index.htm

[解决办法]
sql = "select author,count(name) as totalname from wiki where createdate >= '2008-10-01' and createdate <= '2008-10-30' group by author"

require "dbi"
dbh = DBI.connect("DBI:ODBC:#{db_name}",@user,@password)
sth = dbh.execute(sql)

读书人网 >Ruby Rails

热点推荐