使用 Ruby 连接到 Oracle DB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9892301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:41:27  来源:igfitidea点击:

Connecting to Oracle DB using Ruby

rubyoracledatabase-connectionoci

提问by qwebek

I am stuck with connecting to Oracle DB, have read lots of stuff but no help on result.
I have remote Oracle DB, I am connecting to it using DBVisualizer setting connection like this:

我坚持连接到 Oracle DB,已经阅读了很多东西,但对结果没有帮助。
我有远程 Oracle DB,我使用 DBVisualizer 设置连接连接到它,如下所示:

DB Type : Oracle
Driver (jdbc) : Oracle thin
Database URL: jdbc:oracle:thin:@10.10.100.10:1521/VVV.LOCALDOMAIN
UserIdf: SomeUser
Pass: SomePass

Connection works ok.

连接工作正常。

What I do in Ruby is :

我在 Ruby 中所做的是:

require 'oci8'
require 'dbi'
...

conn = OCI8.new('SomeUser','SomePass','//10.10.100.10:1521/VVV.LOCALDOMAIN')
...

What I get is:

我得到的是:

ORA-12545: Connect failed because target host or object does not exist
oci8.c:360:in oci8lib.so

回答by peter

the third parameter needs to be the TNS hostname, if you use SQL plus it is also the third parameter in the connectstring, you can find it also in the tnsnames.ora file in the oracle maps

第三个参数需要是TNS主机名,如果你使用SQL加上它也是connectstring中的第三个参数,你也可以在oracle maps的tnsnames.ora文件中找到它

in SQLPlus :connect user/password@hostname;
in oci8 :conn = OCI8.new('SomeUser','SomePass',hostname)

在 SQLPlus 中:连接用户/密码@主机名;
在 oci8 中:conn = OCI8.new('SomeUser','SomePass',hostname)

Here a working sample, obfuscated the parameters of course

这是一个工作示例,当然混淆了参数

require 'oci8'
oci = OCI8.new('****','***','****.***')
oci.exec('select * from table') do |record|
  puts record.join(',')
end

回答by durgaprasad vakacharla

require 'oci8'
oci = OCI8.new('system','prasad','127.0.0.1:1521')
oci.exec("CREATE TABLE states1 (
           id CHAR(2) PRIMARY KEY,
           name VARCHAR2(15) NOT NULL,
           capital VARCHAR2(25) NOT NULL)")

回答by durgaprasad vakacharla

require 'oci8'
oci = OCI8.new('system','prasad','127.0.0.1:1521')
oci.exec("insert into states1  values(1,'prasad','visakhapatnam')")
oci.exec("commit")
oci.exec('select * from states1') do |record|
    puts record.join(',')
end