如何使用 NuoDB 在 Ruby On Rails 中手动执行 SQL 命令 [英] How do you manually execute SQL commands in Ruby On Rails using NuoDB

查看:15
本文介绍了如何使用 NuoDB 在 Ruby On Rails 中手动执行 SQL 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试手动执行 SQL 命令,以便我可以访问 NuoDB 中的过程.

I'm trying to manually execute SQL commands so I can access procedures in NuoDB.

我正在使用 Ruby on Rails,并且正在使用以下命令:

I'm using Ruby on Rails and I'm using the following command:

ActiveRecord::Base.connection.execute("SQL query")

SQL 查询"可以是任何 SQL 命令.

The "SQL query" could be any SQL command.

例如,我有一个名为反馈"的表,当我执行命令时:

Like for example I have a table called "Feedback" and when I execute the command:

ActiveRecord::Base.connection.execute("SELECT `feedbacks`.* FROM `feedbacks`")

这只会返回真实"响应,而不是向我发送请求的所有数据.

This would only return a "true" response instead of sending me all the data requested.

这是 Rails 控制台上的输出:

This is the output on the Rails Console is:

SQL (0.4ms)  SELECT `feedbacks`.* FROM `feedbacks`
 => true

我想用它来调用 NuoDB 中的存储过程,但在调用这些过程时,这也会返回真"响应.

I would like to use this to call stored procedures in NuoDB but upon calling the procedures, this would also return a "true" response.

无论如何我可以执行 SQL 命令并获取请求的数据而不是获取真实"响应?

Is there anyway I can execute SQL commands and get the data requested instead of getting a "true" response?

推荐答案

我用来执行自定义 SQL 语句的工作命令是:

The working command I'm using to execute custom SQL statements is:

results = ActiveRecord::Base.connection.execute("foo")

foo"是 sql 语句(即SELECT * FROM table").

with "foo" being the sql statement( i.e. "SELECT * FROM table").

此命令将返回一组值作为散列并将它们放入结果变量中.

This command will return a set of values as a hash and put them into the results variable.

所以在我的 rails application_controller.rb 我添加了这个:

So on my rails application_controller.rb I added this:

def execute_statement(sql)
  results = ActiveRecord::Base.connection.execute(sql)

  if results.present?
    return results
  else
    return nil
  end
end

使用execute_statement将返回找到的记录,如果没有,则返回nil.

Using execute_statement will return the records found and if there is none, it will return nil.

这样我就可以在 rails 应用程序的任何地方调用它,例如:

This way I can just call it anywhere on the rails application like for example:

records = execute_statement("select * from table")

"execute_statement" 还可以调用 NuoDB 过程、函数和数据库视图.

"execute_statement" can also call NuoDB procedures, functions, and also Database Views.

这篇关于如何使用 NuoDB 在 Ruby On Rails 中手动执行 SQL 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆