如何使用 NuoDB 在 Ruby On Rails 中手动执行 SQL 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22752777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:30:56  来源:igfitidea点击:

How do you manually execute SQL commands in Ruby On Rails using NuoDB

sqlruby-on-rails-3activerecordnuodb

提问by Patrick Angodung

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

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

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

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

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

The "SQL query" could be any SQL command.

“SQL 查询”可以是任何 SQL 命令。

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.

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

This is the output on the Rails Console is:

这是 Rails 控制台上的输出:

SQL (0.4ms)  SELECT `feedbacks`.* FROM `feedbacks`
 => 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.

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

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

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

回答by Patrick Angodung

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

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

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

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

“foo”是 sql 语句(即“SELECT * FROM table”)。

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

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

So on my rails application_controller.rb I added this:

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

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

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

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

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

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

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

records = execute_statement("select * from table")

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

“execute_statement”还可以调用 NuoDB 过程、函数以及数据库视图。

回答by Tim Park

For me, I couldn't get this to return a hash.

对我来说,我无法让它返回哈希值。

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

But using the exec_query method worked.

但是使用 exec_query 方法有效。

results = ActiveRecord::Base.connection.exec_query(sql)

回答by MFredrickson-NuoDB

Reposting the answer from our forum to help others with a similar issue:

重新发布我们论坛的答案以帮助其他人解决类似问题:

@connection = ActiveRecord::Base.connection
result = @connection.exec_query('select tablename from system.tables')
result.each do |row|
puts row
end

回答by Andreas Rayo Kniep

res = ActiveRecord::Base.connection_pool.with_connection { |con| con.exec_query( "SELECT 1;" ) }

The above code is an example for

上面的代码是一个例子

  1. executing arbitrary SQL on your database-connection
  2. returning the connection back to the connection pool afterwards
  1. 在您的数据库连接上执行任意 SQL
  2. 之后将连接返回到连接池