我以最基本的方式使用Sequel
gem - 连接到本地数据库并提取结果。
唯一的问题是我的代码依赖于我能够一次执行多个查询语句(将它们拆分不是一个选项)。
我
以下是我的脚本。
require 'sequel'
conn = Sequel.connect(:adapter => 'mysql2', :database=>'my_test_db', :user => 'some_user', :password => 'xxxx', :host => 'localhost', :flags => ::Mysql2::Client::MULTI_STATEMENTS)
# res will be a Sequel::Mysql2::Dataset object
res = conn["select * from table_1; select * from table_2;"]
我可以轻松地通过简单地完成第一个查询的结果(从table_1
中选择)
res.all
或res.each{ |r| puts r }
我的问题是,如何获得下一组结果(从table_2
中选择)?有没有办法存储/缓存现有结果并转到下一个数据集?
尝试再次运行res.all
会导致错误
Sequel::DatabaseDisconnectError: Mysql2::Error: Commands out of sync; you can't run this command now
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `query'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `block in _execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/logging.rb:33:in `log_yield'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `_execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `block in execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/connecting.rb:229:in `block in synchronize'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/connecting.rb:229:in `synchronize'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:795:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:181:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:152:in `fetch_rows'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:143:in `each'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:46:in `all'
from (irb):14
from /Users/lefthandpisces/.rvm/rubies/ruby-1.9.3-p448/bin/irb:12:in `<main>'
谢谢!
答案 0 :(得分:1)
你无法使用mysql2适配器执行此操作。我相信mysql适配器支持它,但即便如此,这也是一个坏主意。使用单独的数据集或单个数据集与UNION。