perl訪問sybase等數據庫

1、配置 sybase環境html

這裏sybase安裝目錄爲/opt/sybasemysql

#
# SAP Product Environment variables
#
SAP_JRE7_32="/opt/sybase/shared/SAPJRE-7_1_027_32BIT"
export SAP_JRE7_32
SAP_JRE7="/opt/sybase/shared/SAPJRE-7_1_027_64BIT"
export SAP_JRE7
SAP_JRE7_64="/opt/sybase/shared/SAPJRE-7_1_027_64BIT"
export SAP_JRE7_64
SYBASE_OCS="OCS-16_0"
export SYBASE_OCS
INCLUDE="/opt/sybase/OCS-16_0/include":$INCLUDE
export INCLUDE
LIB="/opt/sybase/OCS-16_0/lib":$LIB
export LIB
PATH="/opt/sybase/OCS-16_0/bin":$PATH
export PATH
#
# Replace lib, lib3p, and lib3p64 with devlib, devlib3p, and devlib3p64 when debugging
#
LD_LIBRARY_PATH="/opt/sybase/OCS-16_0/lib:/opt/sybase/OCS-16_0/lib3p64:/opt/sybase/OCS-16_0/lib3p":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
LD_LIBRARY_PATH="/opt/sybase/DataAccess/ODBC/lib":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
LD_LIBRARY_PATH="/opt/sybase/DataAccess64/ODBC/lib":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
SYBASE="/opt/sybase"
export SYBASE
SYBASE_ASE="ASE-16_0"
export SYBASE_ASE
PATH="/opt/sybase/ASE-16_0/bin:/opt/sybase/ASE-16_0/install":$PATH
export PATH
SYBROOT="/opt/sybase"
export SYBROOT
SYBASE_JRE_RTDS="/opt/sybase/shared/SAPJRE-7_1_027_64BIT"
export SYBASE_JRE_RTDS
LD_LIBRARY_PATH="/opt/sybase/ASE-16_0/lib":$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
SYBASE_WS="WS-16_0"
export SYBASE_WS
PATH="/opt/sybase/ASE-16_0/jobscheduler/bin":$PATH
export PATH

把上面這段加到~/.bashrcsql

2、安裝 DBI數據庫

wget https://cpan.metacpan.org/authors/id/T/TI/TIMB/DBI-1.636.tar.gzbash

tar zxvf DBI-1.636.tar.gz服務器

cd DBI-1.636測試

perl Makefile.PL
make
sudo make installfetch

3、安裝DBD-Sybasedebug

wget http://www.cpan.org/authors/id/M/ME/MEWP/DBD-Sybase-1.15.tar.gzcode

tar zxvf  DBD-Sybase-1.15.tar.gz

cd DBD-Sybase-1.15

perl Makefile.PL
make
sudo make install

4、安裝其餘DBD模塊注意

DBD-pg須要設置POSTGRES_HOME、POSTGRES_INCLUDE、POSTGRES_LIB等環境變量

DBD-mysql需提供mysql_config路徑,如:

perl Makefile.PL --mysql_config=/opt/mysql/bin/mysql_config

DBD-db2須要配置export DB2_HOME=/home/db2inst1/sqllib,DBD-Oracle須要配置ORACLE_HOME

等,總之編譯安裝DBD模塊前要告知數據庫服務器安裝的位置

5、測試代碼(需修改服務名、數據庫名、用戶名、密碼、表名等對應你的數據庫環境)

#!/usr/bin/perl

use DBI;
use DBD::Sybase; 

$dbname="your-db";
$user="your-user";
$passwd="your-passwd";

$dsn = "DBI:Sybase:server=MYMOTIFVOSTRO145480;database=$dbname";
 

$dbh = DBI->connect($dsn,$user,$passwd) or die "can't connect to database : $DBI::errstr";

$sth=$dbh->prepare("select * from your-table");
$sth->execute;
while (@recs=$sth->fetchrow_array) {
print $recs[0].":".$recs[1].":".$recs[2]."\n";
}
$dbh->disconnect;

6、ODBC方式

安裝DBD-ODBC,wget http://www.cpan.org/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.56.tar.gz

安裝過程和上面相似

配置sybase的odbc驅動

/etc/odbcinst.ini追加

[sybase]
Description     = Sybase
Driver         	= /opt/sybase/DataAccess/ODBC/lib/libsybdrvodb.so 
Driver64        	= /opt/sybase/DataAccess64/ODBC/lib/libsybdrvodb.so
FileUsage       	= 1

/etc/odbc.ini追加

[sybtestdb]
Description     = Sybase ODBC Data Source
Driver          = sybase
Server          = localhost
Port            = 5000
Database        = your-db
ServerName      = your-srvname
UserID          = your-user
Password        = your-passwd

測試代碼

#!/usr/bin/perl

use DBI;
use DBD::ODBC; 
#sybtestdb 字串對應於 /etc/odbc.ini 裏面的 [DSN] 
$dsn = "DBI:ODBC:sybtestdb";
 
$dbh = DBI->connect($dsn) or die "can't connect to database : $DBI::errstr";
$sth=$dbh->prepare("select * from you-table");
$sth->execute;

$sth->{'LongTruncOk'} = 1;
while (@recs=$sth->fetchrow_array) {
	print $recs[0].":".$recs[1].":".$recs[2]."\n";
}
$dbh->disconnect;
相關文章
相關標籤/搜索