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

查看:63
本文介绍了如何使用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.

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

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

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

这只会返回 true响应,而不发送给我请求的所有数据。

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

这是Rails Console上的输出:

This is the output on the Rails Console is:

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

我想用它来调用NuoDB中的存储过程,但是在调用过程时,还会返回 true响应。

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命令并获取请求的数据,而不是获得 true响应?

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.

所以在我的应用程序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.

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

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天全站免登陆