Ruby Sequel AS / 400连接问题

时间:2016-03-16 23:40:49

标签: ruby-on-rails ruby ubuntu odbc ibm-midrange

我需要使用ODBC连接连接到AS / 400系统,并且我使用Ruby sequel gem。我的环境只是在虚拟机中运行的Ubuntu 14.04。我能够运行isql -v my.host.com并进入sql控制台。当我尝试从IRB执行此操作时,我收到错误消息。

require 'sequel'
db = Sequel.odbc(:drvconnect=>'driver={IBM i Access ODBC Driver};system=my.host.com;database=MYDBNAME;uid=MYUSERNAME;password=MYPASSWORD;DefaultLibraries=, *usrlibl;authentication=server')
tables = db[:QSYS2__SYSTABLES].where("TABLE_NAME like ?", "SYS%")
tables.each do |record|
  puts record[:table_name]
end

#=> Sequel::DatabaseConnectionError: ODBC::Error: IM002 (0) [unixODBC][Driver Manager]Data source name not found, and no default driver specified
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:21:in `drvconnect'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:21:in `connect'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool.rb:103:in `make_new'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:224:in `make_new'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:197:in `available'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:133:in `_acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:147:in `block in acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `block in sync'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `synchronize'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `sync'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:146:in `acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/database/connecting.rb:251:in `synchronize'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:44:in `execute'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/dataset/actions.rb:952:in `execute'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:97:in `fetch_rows'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/dataset/actions.rb:141:in `each'

我对AS / 400或使用ODBC连接完全不熟悉,但如果isql有效,那么可能是用户权限错误了吗?

这是我的/etc/odbc.ini

[my.host.com]
Description = IBM i Access ODBC Driver
Driver      = IBM i Access ODBC Driver
System      = my.host.com
UserID      = MYUSERNAME
Password    = MYPASSWORD
Naming      = 1
DefaultLibraries = *usrlibl;
Database    = MYDBNAME

我还应该提到AS / 400系统远程位于my.host.com,因此我没有尝试连接到某些本地运行的服务。我可以ping该主机的IP地址。

修改:我已根据以下建议更改了drvconnect中的驱动程序以匹配odbc.ini中的内容。另外我应该注意到我有一个/etc/odbcinst.ini文件,其中包含以下内容

[IBM i Access ODBC Driver]
Description     = IBM i Access for Linux ODBC Driver
Driver      = /opt/ibm/iaccess/lib/libcwbodbc.so
Setup       = /opt/ibm/iaccess/lib/libcwbodbcs.so
Driver64        = /opt/ibm/iaccess/lib64/libcwbodbc.so
Setup64     = /opt/ibm/iaccess/lib64/libcwbodbcs.so
Threading       = 0
DontDLClose     = 1
UsageCount      = 1

[IBM i Access ODBC Driver 64-bit]
Description     = IBM i Access for Linux 64-bit ODBC Driver
Driver      = /opt/ibm/iaccess/lib64/libcwbodbc.so
Setup       = /opt/ibm/iaccess/lib64/libcwbodbcs.so
Threading       = 0
DontDLClose     = 1
UsageCount      = 1

2 个答案:

答案 0 :(得分:0)

你有这个:

$sql = "INSERT INTO $table(stockcode, lastprice, open, close, low, high)
   VALUES ( '".$data[0]."', '".floatval($data[1])."', '".floatval($data[4])."', '".floatval($data[7])."', '".floatval($data[5])."', '".floatval($data[6])."')";

但是你表明了这一点:

require 'sequel'
db = Sequel.odbc(:drvconnect=>'driver={iSeries Access ODBC Driver}; system=my.host.com;...

如果您确实拥有 IBM i Access ODBC驱动程序,那么您应该在代码中引用它,而不是 iSeries Access ODBC驱动程序。< / p>

答案 1 :(得分:0)

想出来!事实证明,当您使用所有连接信息定义/etc/odbc.ini时,您不需要连接字符串。

require 'sequel'
db = Sequel.odbc(drvconnect: '')
tables = db[:QSYS2__SYSTABLES].where("TABLE_NAME like ?", "SYS%")
tables.each { |r| puts r[:table_name] }

这个查询需要花费很长时间在我的机器上运行,但它会返回所有以“SYS”开头的表。我需要对/etc/odbc.ini进行的唯一更改是将DSN重命名为“默认”

[default]
Description = IBM i Access ODBC Driver
Driver      = IBM i Access ODBC Driver
System      = my.host.com
UserID      = MYUSERNAME
Password    = MYPASSWORD
Naming      = 1
DefaultLibraries = *usrlibl;
Database    = MYDBNAME

对于可能遇到此问题的其他任何人来说,这里有一些其他的事情,我一路上努力让事情发生。我的设置是Ubuntu 14.04

  1. 安装unixodbcunixodbc-dev
  2. 我的my.host.com需要添加到我的/etc/hosts,因为它无法在公共DNS上解析。
  3. 我安装的驱动程序文件名称为ibm-iaccess-1.1.0.4-1.0.amd64.deb
  4. 正在运行ldd /opt/ibm/iaccess/lib64/libcwbodbc.so表示缺少符号链接。
  5. /usr/lib/x86_64-linux-gnu/libodbcinst.so.1
  6. 添加了/usr/lib/x86_64-linux-gnu/libodbcinst.so.2缺少的符号链接

    就是这样。希望能帮助其他人。