MySQL中的一个简单存储过程:
CREATE PROCEDURE `proc01`()
BEGIN
SELECT * FROM users;
END
Run Code Online (Sandbox Code Playgroud)
启动Rails控制台:
$ script/console
Loading development environment (Rails 2.3.5)
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x10343efa0>
Run Code Online (Sandbox Code Playgroud)
看起来不错.但是,通过现有连接再次调用同一存储过程将导致命令不同步错误:
>> 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
Run Code Online (Sandbox Code Playgroud)
可以通过"重新加载"清除错误!控制台中的命令:
>> reload!
Reloading...
=> true
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x1033f14d0>
>>
Run Code Online (Sandbox Code Playgroud)
如何从Rails调用MySQL存储过程?
我有阵列:
array(2=>'0',3=>'1',5=>'3',4=>'4',7=>'2')
Run Code Online (Sandbox Code Playgroud)
注意:似乎没有任何顺序.因此,请保留计划以查找数字顺序.
如何修复此关联数组的第i个元素?
例如,当i是4该值应为4,当i是1该值应0.
array_shift()从来没有解决这个要求.很清楚循环逻辑解决了这个问题.