我需要编写一个独立的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)

我错过了什么?

SOLUTION

在从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

推荐答案

也许可以试试这个:

ActiveRecord::Base.establish_connection(...)
ActiveRecord::Base.connection.execute(...)

Sql相关问答推荐

如何从上一个值减go 值

如何在不更改S代码的情况下,判断存储过程调用了多少次clr函数?

冲突查询的UPDATE时违反非空约束

重用传递给 node 的参数-postgres upsert查询

MS Access问题查询中的自定义字段

SQL:如果一个表中的日期在另一个表的日期的12个月内,则为是或否

SAS proc freq 或 proc sql 获取数据子集和整个数据的频率

清理 XML 数据

有没有办法在雅典娜中将字符串转换为 int ?

PostgreSQL中如何提取以特定字符开头的字符串中的所有单词?

基于 Snowflake 的最大值创建新列

根据是否存在值组合分组并 Select 行

我如何编写一个遍历数组数组并将所有值连接成一个字符串的 postgres 函数

在 PostgreSQL 中使用重音敏感排序进行重音不敏感搜索

如何将多行的查询结果合并为一行

如何在 PL/SQL 中区分返回的 XML 值?

SQL 计数和过滤查询优化

具有关联统计信息 N+1 的 Rails 6 索引资源?

在整个递归级别上终止递归查询 - Postgres

除以表中的几个元素