濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫(kù)的方法

Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫(kù)的方法

熱門(mén)標(biāo)簽:萬(wàn)利達(dá)百貨商場(chǎng)地圖標(biāo)注 okcc外呼系統(tǒng)怎么調(diào)速度 電話機(jī)器人哪里有賣(mài) 熱門(mén)電銷(xiāo)機(jī)器人 上海企業(yè)外呼系統(tǒng) 河南虛擬外呼系統(tǒng)公司 外呼電信系統(tǒng) 惠州龍門(mén)400電話要怎么申請(qǐng) 智能機(jī)器人電銷(xiāo)神器

本文將提供一些perl連接Microsoft SQL Server數(shù)據(jù)庫(kù)的實(shí)例。perl腳本運(yùn)行在Windows和Linux平臺(tái)。

Windows平臺(tái)

如果在Windows平臺(tái)下運(yùn)行perl腳本,建議使用依賴DBI的兩個(gè)模塊包,提供標(biāo)準(zhǔn)的數(shù)據(jù)庫(kù)接口模塊。

DBD::ODBC
DBD::ADO

使用DBD::ODBC

如果選用DBD::ODBC,下面的實(shí)例代碼將展示如何連接到SQL Server數(shù)據(jù)庫(kù):

復(fù)制代碼 代碼如下:

use DBI;
 
# DBD::ODBC
 
my $dsn = 'DBI:ODBC:Driver={SQL Server}';
my $host = '10.0.0.1,1433';
my $database = 'my_database';
my $user = 'sa';
my $auth = ‘s3cr3t';
 
# Connect via DBD::ODBC by specifying the DSN dynamically.
my $dbh = DBI->connect("$dsn;Server=$host;Database=$database",
 $user,
 $auth,
 { RaiseError => 1, AutoCommit => 1}
 ) || die "Database connection not made: $DBI::errstr";
 
#Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees ";
my $sth = $dbh->prepare( $sql );
 
#Execute the statement
$sth->execute();
 
my( $id, $name, $phone_number );
 
# Bind the results to the local variables
$sth->bind_columns( undef, \$id, \$name, \$phone_number );
 
#Retrieve values from the result set
while( $sth->fetch() ) {
 print "$id, $name, $phone_number\n";
}
 
#Close the connection
$sth->finish();
$dbh->disconnect();

你還可以使用預(yù)先設(shè)置的一個(gè)系統(tǒng)DSN來(lái)連接。要建立一個(gè)系統(tǒng)DSN,可以這樣訪問(wèn)控制面板->管理工具->數(shù)據(jù)源。

使用系統(tǒng)DSN連接,需要更改連接字符串。如下所示:

復(fù)制代碼 代碼如下:

# Connect via DBD::ODBC using a System DSN
my $dbh = DBI->connect("dbi:ODBC:my_system_dsn",
 $user,
 $auth,
 {
 RaiseError => 1,
 AutoCommit => 1
 }
 ) || die "Database connection not made: $DBI::errstr";

使用DBD::ADO

如果選擇DBD::ADO模塊,下面的實(shí)例展示如何連接到SQL Server數(shù)據(jù)庫(kù)。

復(fù)制代碼 代碼如下:

use DBI;
 
my $host = '10.0.0.1,1433';
my $database = 'my_database';
my $user = 'sa';
my $auth = ‘s3cr3t';
 
# DBD::ADO
$dsn = "Provider=sqloledb;Trusted Connection=yes;";
$dsn .= "Server=$host;Database=$database";
my $dbh = DBI->connect("dbi:ADO:$dsn",
 $user,
 $auth,
 { RaiseError => 1, AutoCommit => 1}
 ) || die "Database connection not made: $DBI::errstr";
 
#Prepare a SQL statement
my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql );
 
#Execute the statement
$sth->execute();
 
my( $id, $name, $phone_number );
 
# Bind the results to the local variables
$sth->bind_columns( undef, \$id, \$name, \$phone_number );
 
#Retrieve values from the result set
while( $sth->fetch() ) {
 print "$id, $name, $phone_number\n";
}
 
#Close the connection
$sth->finish();
$dbh->disconnect();

Linux平臺(tái)

如果是在Linux平臺(tái)下運(yùn)行perl腳本,連接SQL Server數(shù)據(jù)庫(kù)需要使用到DBD::Sybase包。

安裝SQL Server支持庫(kù)

Sybase DBD包依賴FreeTDS驅(qū)動(dòng)程序。

FreeTDS下載地址:www.freetds.org

安裝FreeTDS驅(qū)動(dòng)的說(shuō)明文檔參見(jiàn):http://www.freetds.org/userguide/config.htm

該驅(qū)動(dòng)沒(méi)有使用到ODBC.

配置數(shù)據(jù)源

修改freetds.conf文件包括SQL Server數(shù)據(jù)庫(kù)信息,如下所示:

復(fù)制代碼 代碼如下:

[SS_MY_DB]
host = 10.0.0.1 # or host name port = 1433
tds version = 7.0

安裝Sybase DBD模塊

該模塊文檔參見(jiàn):http://search.cpan.org/~mewp/DBD-Sybase/Sybase.pm

此外,需要將sybase環(huán)境變量應(yīng)設(shè)置為FreeTDS安裝路徑,export SYBASE=/usr/local/freetds

使用Sybase DBI和SQL Server DSN實(shí)例

復(fù)制代碼 代碼如下:

# load the DBI module
use DBI;
use DBD::Sybase;
 
my $database="my_database";
my $user="sa";
my $auth="s3cr3t";
 
BEGIN
{
 $ENV{SYBASE} = "/usr/local";
}
 
# Connect to the SQL Server Database
my $dbh = DBI->connect("dbi:Sybase:server=ss_my_db;database=$database",
 $user,
 $auth
 {RaiseError => 1, AutoCommit => 1}
 ) || die "Database connection not made: $DBI::errstr";
 
#Prepare a SQL statement
my $sql = "SELECT id, name, phone_number FROM employees";
my $sth = $dbh->prepare( $sql );
 
#Execute the statement
$sth->execute();
 
my( $id, $name, $phone_number );
 
# Bind the results to the local variables
$sth->bind_columns( undef, \$id, \$name, \$phone_number );
 
#Retrieve values from the result set
while( $sth->fetch() ) {  print "$name, $title, $phone\n";
}
 
#Close the connection
$sth->finish();
undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase
$dbh->disconnect();

您可能感興趣的文章:
  • 讓apache2以cgi方式運(yùn)行perl cgi程序的實(shí)現(xiàn)方法
  • windows下Apache+MySql+PHP3+PHP4+PERL安裝配置
  • Win2000+Apache+MySql+PHP4+PERL安裝使用小結(jié)
  • Windows Server 2016 上配置 APACHE+SSL+PHP+perl的教程詳解

標(biāo)簽:百色 綏化 周口 淮安 合肥 綿陽(yáng) 秦皇島 周口

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫(kù)的方法》,本文關(guān)鍵詞  Windows,和,Linux,系統(tǒng),下,perl,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫(kù)的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫(kù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    平远县| 新余市| 确山县| 阳曲县| 恩平市| 娱乐| 沂源县| 边坝县| 石首市| 叶城县| 运城市| 盐山县| 墨竹工卡县| 泊头市| 兴业县| 通河县| 合水县| 西乌珠穆沁旗| 客服| 四川省| 淮安市| 东兴市| 从江县| 武威市| 桐梓县| 耒阳市| 新营市| 泰兴市| 西安市| 北辰区| 合水县| 惠水县| 易门县| 叶城县| 札达县| 德安县| 堆龙德庆县| 舒兰市| 阿克苏市| 凉山| 雷波县|