• <tfoot id='cNHEu'></tfoot>

    <small id='cNHEu'></small><noframes id='cNHEu'>

    <i id='cNHEu'><tr id='cNHEu'><dt id='cNHEu'><q id='cNHEu'><span id='cNHEu'><b id='cNHEu'><form id='cNHEu'><ins id='cNHEu'></ins><ul id='cNHEu'></ul><sub id='cNHEu'></sub></form><legend id='cNHEu'></legend><bdo id='cNHEu'><pre id='cNHEu'><center id='cNHEu'></center></pre></bdo></b><th id='cNHEu'></th></span></q></dt></tr></i><div id='cNHEu'><tfoot id='cNHEu'></tfoot><dl id='cNHEu'><fieldset id='cNHEu'></fieldset></dl></div>

    • <bdo id='cNHEu'></bdo><ul id='cNHEu'></ul>

      <legend id='cNHEu'><style id='cNHEu'><dir id='cNHEu'><q id='cNHEu'></q></dir></style></legend>

      1. 如何从 Rails 调用 MySQL 存储过程?

        How to call MySQL stored procedure from Rails?(如何从 Rails 调用 MySQL 存储过程?)

          <bdo id='K4qjQ'></bdo><ul id='K4qjQ'></ul>

            • <small id='K4qjQ'></small><noframes id='K4qjQ'>

                <tbody id='K4qjQ'></tbody>
              1. <tfoot id='K4qjQ'></tfoot>
                <i id='K4qjQ'><tr id='K4qjQ'><dt id='K4qjQ'><q id='K4qjQ'><span id='K4qjQ'><b id='K4qjQ'><form id='K4qjQ'><ins id='K4qjQ'></ins><ul id='K4qjQ'></ul><sub id='K4qjQ'></sub></form><legend id='K4qjQ'></legend><bdo id='K4qjQ'><pre id='K4qjQ'><center id='K4qjQ'></center></pre></bdo></b><th id='K4qjQ'></th></span></q></dt></tr></i><div id='K4qjQ'><tfoot id='K4qjQ'></tfoot><dl id='K4qjQ'><fieldset id='K4qjQ'></fieldset></dl></div>

                <legend id='K4qjQ'><style id='K4qjQ'><dir id='K4qjQ'><q id='K4qjQ'></q></dir></style></legend>

                1. 本文介绍了如何从 Rails 调用 MySQL 存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  MySQL 中一个简单的存储过程:

                  A simple stored procedure in MySQL:

                  CREATE PROCEDURE `proc01`()
                  BEGIN
                   SELECT * FROM users;
                  END
                  

                  启动 Rails 控制台:

                  Starts Rails console:

                  $ script/console
                  Loading development environment (Rails 2.3.5)
                  >> User.connection.execute("CALL proc01")
                  => #<Mysql::Result:0x10343efa0>
                  

                  看起来不错.但是,通过现有连接再次调用相同的存储过程将导致命令不同步错误:

                  Looks good. BUT, any more call to the same stored procedure via the existing connection will result in an Commands out of sync error:

                  >> User.connection.execute("CALL proc01")
                  ActiveRecord::StatementInvalid: Mysql::Error: Commands out of sync; you can't run this command now: CALL proc01
                      from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
                      from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
                      from (irb):2
                  

                  错误可以通过重新加载!"清除.控制台中的命令:

                  The error can be cleared by a "reload!" command in the console:

                  >> reload!
                  Reloading...
                  => true
                  >> User.connection.execute("CALL proc01")
                  => #<Mysql::Result:0x1033f14d0>
                  >> 
                  

                  如何从 Rails 调用 MySQL 存储过程?

                  How can I call MySQL stored procedure from Rails?

                  推荐答案

                  --

                  使用 ActiveRecord::Base.connections.exec_query() 是我可以告诉 很多 更好的方法,因为它返回一组哈希值期望,ActiveRecord::Base.connections.execute 没有.

                  Using ActiveRecord::Base.connections.exec_query() is as far as I can tell a MUCH better approach just because it returns an array of hashes as one would expect, which ActiveRecord::Base.connections.execute does not.

                  文档

                  --

                  请阅读上面的编辑,我将保留下面的内容以供参考.

                  虽然我意识到这个问题已经很老了,而且因为 ohho 发布的链接有 404'd,但我最近遇到了同样的错误.

                  While I realise this question is quite old and because the links ohho posted have 404'd, I had this same error recently.

                  我能够通过执行以下操作来修复它:

                  I was able to fix it by doing the following:

                  result = ActiveRecord::Base.connection.execute("调用example_proc()")ActiveRecord::Base.clear_active_connections!

                  清除连接后,您可以运行任何其他查询,而之前尝试通过 rails 或其他存储过程访问数据库时会失败.

                  Once you've cleared connections, you can run any other queries where as before it would have failed on trying to access the database through rails or another stored proc.

                  http://apidock.com/rails/v3.2.13/ActiveRecord/Base/clear_active_connections%21/class

                  --

                  还值得一提的是,根据 leente 在此 链接

                  It's also worth mentioning that one shouldn't store the ActiveRecord connection in a variable as per leente's post on this link

                  不要缓存它!

                  不要将连接存储在变量中,因为另一个线程可能会在它已经签回连接池时尝试使用它.请参阅:ConnectionPool"

                  Don’t store a connection in a variable, because another thread might try to use it when it’s already checked back in into the connection pool. See: ConnectionPool"

                  connection = ActiveRecord::Base.connection   #WRONG
                  
                  threads = (1..100).map do
                   Thread.new do
                  begin
                    10.times do
                      connection.execute("SELECT SLEEP(1)")  # WRONG
                      ActiveRecord::Base.connection.execute("SELECT SLEEP(1)")  # CORRECT
                    end
                    puts "success"
                  rescue => e
                    puts e.message
                     end
                    end
                  end
                  
                  threads.each(&:join) 
                  

                  这篇关于如何从 Rails 调用 MySQL 存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Accessing another user#39;s table within an Oracle Stored Procedure(在 Oracle 存储过程中访问另一个用户的表)
                  Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)(超出最大存储过程、函数、触发器或视图嵌套级别(限制 32))
                  How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                  How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?(如何使用 MyBatis 将 Java 对象列表传递给 Oracle 存储过程?)
                  Set the variable result, from query(设置变量结果,来自查询)
                  What is dynamic SQL?(什么是动态 SQL?)
                    <bdo id='BrzW7'></bdo><ul id='BrzW7'></ul>

                      1. <tfoot id='BrzW7'></tfoot>

                          <small id='BrzW7'></small><noframes id='BrzW7'>

                            <tbody id='BrzW7'></tbody>

                            <legend id='BrzW7'><style id='BrzW7'><dir id='BrzW7'><q id='BrzW7'></q></dir></style></legend>

                            <i id='BrzW7'><tr id='BrzW7'><dt id='BrzW7'><q id='BrzW7'><span id='BrzW7'><b id='BrzW7'><form id='BrzW7'><ins id='BrzW7'></ins><ul id='BrzW7'></ul><sub id='BrzW7'></sub></form><legend id='BrzW7'></legend><bdo id='BrzW7'><pre id='BrzW7'><center id='BrzW7'></center></pre></bdo></b><th id='BrzW7'></th></span></q></dt></tr></i><div id='BrzW7'><tfoot id='BrzW7'></tfoot><dl id='BrzW7'><fieldset id='BrzW7'></fieldset></dl></div>