我需要编写一个独立的ruby脚本来处理数据库。我在rails 3中使用了下面给出的代码
@connection = ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:database => "siteconfig_development",
:username => "root",
:password => "root123"
)
results = @connection.execute("select * from users")
results.each do |row|
puts row[0]
end
但出现错误:-
`<main>': undefined method `execute' for #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00000002867548> (NoMethodError)
我在这里想念什么?
解
从denis-bu获得解决方案后,我按照以下方式使用了它,它也起作用了。
@connection = ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:database => "siteconfig_development",
:username => "root",
:password => "root123"
)
sql = "SELECT * from users"
@result = @connection.connection.execute(sql);
@result.each(:as => :hash) do |row|
puts row["email"]
end
connection.execute
,而不是connection().execute