结合使用Sybase ASE 12.5.4和jTDS驱动程序以及JRuby [英] Using Sybase ASE 12.5.4 with jTDS drivers with JRuby
问题描述
问题
我正在尝试构建一个小的ruby脚本-每天使用jruby运行一次-连接到Sybase ASE 12.5.4数据库并执行复杂的查询.
I am trying to build a small ruby script - which will be run using jruby once a day - to connect to a Sybase ASE 12.5.4 database and perform a complex query.
最终,我打算对数据进行一些处理,并将新数据插入MySQL表中,以供在Rails应用程序中使用.
Ultimately I intend to do some processing on the data and insert the new data in a MySQL table for use within a rails application.
环境
- jruby v1.4.0
- java v1.6.0_15
- 在Ubuntu Karmic上
JRuby已安装的宝石
- activerecord-jdbc-adapter(0.9.1)
- activerecord-2.3.4
Jruby Lib目录
- jtds-1.2.5
查询
SET rowcount 10
SELECT * FROM TEST_TABLE
代码段
require 'java'
require 'jtds-1.2.5.jar'
require 'rubygems'
require 'active_record'
config = {
:username => 'railstest',
:password => 'railstest',
:adapter => 'jdbc',
:dialect => 'sybase',
:host => 'localhost',
:database => 'railstest',
:port => '5000',
:driver => 'net.sourceforge.jtds.jdbc.Driver',
:url => 'jdbc:jtds:sybase://localhost:5000/railstest'
}
ActiveRecord::Base.establish_connection(config).connection.execute(-- QUERY --)
我可以确认这已连接到数据库.虽然我在从数据库表中选择10行时遇到问题.
I can confirm this connects to the DB. Although I am having issues just selecting 10 rows from a database table.
生产
对于执行方法:
/usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log': ActiveRecord::ActiveRecordError: The executeUpdate method must not return a result set.: SET rowcount 10 SELECT * FROM TEST_TABLE (ActiveRecord::StatementInvalid)
from /usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:559:in `execute'
from db-test.rb:21
对于 select_rows 方法:
/usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log': ActiveRecord::ActiveRecordError: The executeUpdate method must not return a result set.: SET rowcount 10 SELECT * FROM TEST_TABLE (ActiveRecord::StatementInvalid)
from /usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:559:in `execute'
from /usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:629:in `select'
from /usr/local/bin/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.2/lib/active_record/connection_adapters/jdbc_adapter.rb:550:in `select_rows'
from db-test.rb:21
该错误指出我不应该返回结果集,但是无论我使用,执行,select_rows等哪种方法都无济于事.
The error states that I should not return a results set but it doesn't matter which method I use, execute, select_rows etc nothing works.
关于查询的另一件事.我的原始查询相当复杂,我对变量进行了十进制处理,删除了临时表并创建了临时表,然后填充并从中选择.使用Squirrel SQL,我可以执行一次并获得结果.使用DBI,我无法在一次执行中执行此操作,有人知道我是否只能执行一次整个操作,还是必须将其拆分?
One more thing regarding queries. My original query is rather complex, I decalre variables, drop temporary tables and create temporary tables as well as populate and select from them. Using Squirrel SQL I can execute once and gain a result. Using DBI I was unable to do this in one execution, does anyone know if I can just execute the whole thing once or will I have to split it up?
请问有人可以给我任何帮助吗?我是否正确使用jTDS? 提前谢谢了.
Would anyone be able to give me any assistance please? Am I using jTDS properly? Many thanks in advance.
推荐答案
一段时间以来,我一直在使用Sybase驱动程序,如@lollipopman所建议的那样,这对入门很有帮助,但是当我构建更复杂的查询时,我一直遇到问题,因此我试图重新研究最初的问题,并且花了一个小时左右的时间使它正常工作.
I had been using the Sybase drivers for sometime as suggested by @lollipopman which was helpful in getting going but as I built more complex queries I kept running into issues so I tried to revisit the original problem and with an hour or so I go it working.
在此处找到
require java
require jtds-1.2.5.jar
require rubygems
require dbi
dbh = DBI.connect('dbi:Jdbc:jtds:sybase://<host>:<port>/<db>', <username>, <password>, {'driver' => 'net.sourceforge.jtds.jdbc.Driver'} )
这就是使用JRuby和DBI连接到Sybase数据库所需的全部
And that is all that is needed to connect to your Sybase Database with JRuby and DBI
希望这对某人有帮助!
这篇关于结合使用Sybase ASE 12.5.4和jTDS驱动程序以及JRuby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!