簡體   English   中英

從 Perl 連接到 Oracle

[英]Connecting to Oracle from Perl

我正在嘗試使用 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 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.

我是否需要對 Oracle Instant Client 執行任何操作,因為它沒有安裝程序。 Perl 中是否還有其他需要配置的內容?

感謝您提供的任何幫助。

* 編輯 *

我需要在 Perl 的開頭設置任何變量以鏈接到 SQL 開發人員或即時客戶端嗎?

我使用不同的方法將 Perl 連接到 Oracle DB。 我使用 SQLPlus 而不是 DBI。 這是來自 Oracle 的命令行實用程序,可以從 Perl 調用。 下面是我的代碼示例。 test.sql 文件可以包含一個或多個查詢,並且必須以退出結束。

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

我在中遇到了同樣的問題,最后可以解決。

就我而言,事實證明(經過大量調查和閱讀)問題的根源在於版本的混合。 The server was 11.2 meanwhile I used the 12.1 Instant Client package to build Oracle.dll for DBD::Oracle . 所以我下載了 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。 這被添加到 PATH 中,因為 Windows 查找 PATH 以查找 DLL 而不是 LD_LIBRARY_PATH。 文件tnsnames.ora可以在$TNS_ADMIN目錄中,或者在/var/opt/oracle/etc (或其他一些地方)中。 定義的服務名稱可以通過DBI->data_sources('Oracle')列出。

瞧! 我希望這會有所幫助!

使用該錯誤消息和您的代碼,我首先檢查它是否有助於在$db=DBI->connect(..的第一個參數中明確state Servername (如果需要,谷歌獲取一些示例)。

如果這沒有幫助,我會檢查環境變量OCI_NLS_CHARSET_ID的值。

我想連接到在 Docker 容器中運行的 Oracle XE(端口 1521 和 5500 映射到它們的主機等效項)並查詢 Z30162ED78B6C10F7314FZ 示例數據庫。 這對我有用。

#!/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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM