前面说到了《perl如何连接SQL Server数据库》,下面来实际应用下。
安装FreeTDS
1
2
3
4
5
6
7
|
# yum install freetds.x86_64
# yum install freetds-devel.x86_64
# vim /etc/freetds.conf
[
SQLSERVER
]
host
=
10.0.1.1
#SQL server服务器地址
port
=
1433
tds
version
=
7.0
|
需要先安装EPEL源,EPEL源如何安装可以参见本博客以前内容。
测试
1
2
3
4
|
# tsql -S sqlserver.hostname.com -U sqlusername -D your_db_name
select *
from
sys
.
Tables
;
GO
quit
|
安装DBD::Sybase
1
2
|
# export SYBASE=/usr
# cpanm DBD::Sybase
|
cpanm安装可以参见本博客之前内容。
perl实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/perl
#use lib ('./modules');
use
DBI
;
use
DBD
::
Sybase
;
my
$
database
=
"your_db_name"
;
my
$
username
=
'sqlusername'
;
my
$
password
=
'sqlpassword'
;
#SQLSERVER 字串对应于 /etc/freetds.conf 里面的 [SQLSERVER]
$
dsn
=
"DBI:Sybase:server=SQLSERVER;database=$database"
;
my
$
dbh
=
DBI
->
connect
(
$
dsn
,
$
username
,
$
password
)
or
die
"Database connection not made : $DBI::errstr"
;
my
$
sql
=
"SELECT TOP 5 * FROM httpstatus order by testtime desc"
;
my
$
sth
=
$
dbh
->
prepare
(
$
sql
)
;
$
sth
->
execute
(
)
or
die
"unable to execute query $sql, error $DBI::errstr\n"
;
$
rows
=
$
sth
->
rows
;
while
(
@
first
=
$
sth
->
fetchrow
_array
)
{
foreach
$
field
(
@
first
)
{
print
"$field "
;
}
print
"\n"
;
}
$
sth
->
finish
(
)
;
undef
$
sth
;
$
dbh
->
disconnect
(
)
;
print
"Query successfully\n"
;
|
收 藏