从 Perl 连接到 Oracle

发布于 2024-11-17 05:38:55 字数 739 浏览 5 评论 0原文

我正在尝试使用 Perl 连接到 Oracle。

我正在尝试从安装了 Perl 的 Windows XP 计算机进行连接。我还下载了 Oracle SQL Developer 和 Oracle Instant Client。我可以使用 TNS 连接类型使用 Oracle SQL Developer 连接到 Oracle DB。

我使用以下 Perl。

use DBI;
$db=DBI->connect( "dbi:Oracle", "username", "password" ) or die "Can't connect $DBI::errstr\n";

我收到以下错误消息。

DBI 连接('','用户名',...)失败:错误
OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) 在 oracle2.pl 第 3 行检查 NLS 设置等
无法连接到数据库错误 OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) 检查 NLS 设置等。

由于 Oracle Instant Client 没有安装程序,我是否需要对其执行任何操作? Perl 中还需要配置什么吗?

是否需要在 Perl 开头设置任何变量才能链接到 SQL Developer 或 Instant Client?

I am trying to connect to Oracle using Perl.

I am trying to connect from a Windows XP machine that has Perl installed. I also have downloaded Oracle SQL Developer and Oracle Instant Client. I can connect to the Oracle DB using Oracle SQL Developer using the TNS connection type.

I use the following Perl.

use DBI;
$db=DBI->connect( "dbi:Oracle", "username", "password" ) or die "Can't connect $DBI::errstr\n";

I get the following error message.

DBI connect('','username',...) failed: ERROR
OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) Check NLS settings etc. at oracle2.pl line3
Cant connect to database ERROR OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) Check NLS settings etc.

Do I need to do anything with Oracle Instant Client because it does not have an installer? Is there something else that needs to be configured in Perl?

Are there any variables I need to set at the beginning of the Perl to link to either SQL Developer or Instant Client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏雨一样淡蓝的深情 2024-11-24 05:38:55

我使用了不同的方法将 Perl 连接到 Oracle DB。我使用 SQLPlus 而不是 DBI。这是来自 Oracle 的命令行实用程序,可以从 Perl 调用。下面是我的代码示例。 test.sql 文件可以包含一个或多个查询,并且必须以 exit 结束。

my $connect_string = 'username/password@server'; # connection to the DB
my $file = 'test.sql'; # location of SQL file. The file must end with "exit"

my $sqlcmd = "sqlplus -S $connect_string \@$file"; # sqlcommand
system $sqlcmd; # executes command

I used a different method to connect Perl to an Oracle DB. I used SQLPlus instead of DBI. This is a command line utility from Oracle that can be called from Perl. Below is a sample of my code. The test.sql file can contain one or multiple queries and must end with exit.

my $connect_string = 'username/password@server'; # connection to the DB
my $file = 'test.sql'; # location of SQL file. The file must end with "exit"

my $sqlcmd = "sqlplus -S $connect_string \@$file"; # sqlcommand
system $sqlcmd; # executes command
等待我真够勒 2024-11-24 05:38:55

有了该错误消息和您的代码段,我首先检查它是否有助于在 $db=DBI->connect(..< /code> (如果需要,请谷歌一些示例)。

如果这没有帮助,我会检查环境变量 OCI_NLS_CHARSET_ID 的值。

With that error message and your piece of code, I'd first check whether it helps to explicitely state the Servername in the first parameter of $db=DBI->connect(.. (if need be, google for some examples).

If that doesn't help, I would then check the value of the environmental variable OCI_NLS_CHARSET_ID.

回眸一遍 2024-11-24 05:38:55

我在 和最后遇到了同样的问题这是可以解决的。

就我而言,事实证明(经过大量调查和阅读)问题的根源在于版本的混合。服务器是 11.2,同时我使用 12.1 Instant Client 包为 DBD::Oracle 构建 Oracle.dll。所以我下载了11.2版本(来自Oracle)和此错误消息就这样消失了!

我已阅读故障排除手册,但似乎需要 3 个包:Basic、SDK 和 sqlplus(后者由构建过程用来确定服务器版本)。

在构建之前必须设置一些bash变量:

export ORACLE_HOME=/cygdrive/c/install/instantclient_11_2
PATH+=:"$ORACLE_HOME"
export TNS_ADMIN="$HOME"

稍后需要找到Oracle.dll引用的oci.dll。当 Windows 查找 PATH 来查找 DLL 而不是 LD_LIBRARY_PATH 时,它会添加到 PATH 中。文件 tnsnames.ora 可以位于 $TNS_ADMINdir 中,或者在 /var/opt/oracle/etc (或其他一些地方)。可以通过DBI->data_sources('Oracle')列出定义的服务名称。

瞧!我希望这能有所帮助!

I had the same problem in and at the end it could be solved.

In my case it turned out (after a lot of investigation and reading) that the root of the problem was with mixing of versions. The server was 11.2 meanwhile I used the 12.1 Instant Client package to build Oracle.dll for DBD::Oracle. So I downloaded the 11.2 version (from Oracle) and this error message just disappeared!

I have read the Trooble Shooting manual, but it seems 3 packages are needed: Basic, SDK and sqlplus (the later is used by the build process to determine the server version).

Before build some bash variables had to be set:

export ORACLE_HOME=/cygdrive/c/install/instantclient_11_2
PATH+=:"$ORACLE_HOME"
export TNS_ADMIN="$HOME"

The later needed to find oci.dll which is referenced by Oracle.dll. This is added to PATH as Windows looks for PATH to find DLLs not LD_LIBRARY_PATH. File tnsnames.ora can be in $TNS_ADMINdir, or in /var/opt/oracle or in /etc (or some other places). The defined service names can be listed by DBI->data_sources('Oracle').

And voilà! I hope this could help!

尾戒 2024-11-24 05:38:55

我想要连接到在 Docker 容器中运行的 Oracle XE(端口 1521 和 5500 映射到其等效主机)并查询 Oracle 示例数据库。这对我有用。

#!/usr/bin/perl

use DBI;

# connect to Oracle...
$dbh = DBI->connect("dbi:Oracle:localhost/xepdb1","ot","Orcl1234");

# prepare and execute the SQL statement
$sth = $dbh->prepare("SELECT first_name, last_name FROM employees");
$sth->execute;

# retrieve the results
printf "%-30s %-30s\n", "First Name", "Last Name";
while(  my $ref = $sth->fetchrow_hashref() ) {
    printf "%-30s %-30s\n", $ref->{'FIRST_NAME'}, $ref->{'LAST_NAME'};
}
exit;

I wanted to connect to Oracle XE running in a Docker container (with ports 1521 and 5500 mapped to their host equivalents) and query the Oracle sample database. Here's what worked for me.

#!/usr/bin/perl

use DBI;

# connect to Oracle...
$dbh = DBI->connect("dbi:Oracle:localhost/xepdb1","ot","Orcl1234");

# prepare and execute the SQL statement
$sth = $dbh->prepare("SELECT first_name, last_name FROM employees");
$sth->execute;

# retrieve the results
printf "%-30s %-30s\n", "First Name", "Last Name";
while(  my $ref = $sth->fetchrow_hashref() ) {
    printf "%-30s %-30s\n", $ref->{'FIRST_NAME'}, $ref->{'LAST_NAME'};
}
exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文