perl如何连接SQL Server数据库

数据库
本文将提供一些perl连接Microsoft SQL Server数据库的实例。perl脚本运行在Windows和Linux平台。

  本文将提供一些perl连接Microsoft SQL Server数据库的实例。perl脚本运行在Windows和Linux平台。

  Windows平台

  如果在Windows平台下运行perl脚本,建议使用依赖DBI的两个模块包,提供标准的数据库接口模块。 DBD::ODBC DBD::ADO

  使用DBD::ODBC

  如果选用DBD::ODBC,下面的实例代码将展示如何连接到SQL Server数据库: 

  1. use DBI;  
  2.   
  3. # DBD::ODBC  
  4.   
  5. my $dsn = 'DBI:ODBC:Driver={SQL Server}';  
  6. my $host = '10.0.0.1,1433';  
  7. my $database = 'my_database';  
  8. my $user = 'sa';  
  9. my $auth = ‘s3cr3t';  
  10.   
  11. Connect via DBD::ODBC by specifying the DSN dynamically.  
  12. my $dbh = DBI->connect("$dsn;Server=$host;Database=$database",  
  13.  $user,  
  14.  $auth,  
  15.  { RaiseError => 1, AutoCommit => 1}  
  16.  ) || die "Database connection not made: $DBI::errstr";  
  17.   
  18. #Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees ";  
  19. my $sth = $dbh->prepare( $sql );  
  20.   
  21. #Execute the statement  
  22. $sth->execute();  
  23.   
  24. my( $id, $name, $phone_number );  
  25.   
  26. # Bind the results to the local variables  
  27. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  28.   
  29. #Retrieve values from the result set  
  30. while( $sth->fetch() ) {  
  31.  print "$id, $name, $phone_number\n";  
  32. }  
  33.   
  34. #Close the connection  
  35. $sth->finish();  
  36. $dbh->disconnect(); 

 

  你还可以使用预先设置的一个系统DSN来连接。要建立一个系统DSN,可以这样访问控制面板->管理工具->数据源。 使用系统DSN连接,需要更改连接字符串。如下所示:

 

  1. Connect via DBD::ODBC using a System DSN  
  2. my $dbh = DBI->connect("dbi:ODBC:my_system_dsn",  
  3.  $user,  
  4.  $auth,  
  5.  {  
  6.  RaiseError => 1,  
  7.  AutoCommit => 1  
  8.  }  
  9.  ) || die "Database connection not made: $DBI::errstr"

 

  使用DBD::ADO

  如果选择DBD::ADO模块,下面的实例展示如何连接到SQL Server数据库。

 

  1. use DBI;  
  2.   
  3. my $host = '10.0.0.1,1433';  
  4. my $database = 'my_database';  
  5. my $user = 'sa';  
  6. my $auth = ‘s3cr3t';  
  7.   
  8. # DBD::ADO  
  9. $dsn = "Provider=sqloledb;Trusted Connection=yes;";  
  10. $dsn .= "Server=$host;Database=$database";  
  11. my $dbh = DBI->connect("dbi:ADO:$dsn",  
  12.  $user,  
  13.  $auth,  
  14.  { RaiseError => 1, AutoCommit => 1}  
  15.  ) || die "Database connection not made: $DBI::errstr";  
  16.   
  17. #Prepare a SQL statement  
  18. my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql );  
  19.   
  20. #Execute the statement  
  21. $sth->execute();  
  22.   
  23. my( $id, $name, $phone_number );  
  24.   
  25. # Bind the results to the local variables  
  26. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  27.   
  28. #Retrieve values from the result set  
  29. while( $sth->fetch() ) {  
  30.  print "$id, $name, $phone_number\n";  
  31. }  
  32.   
  33. #Close the connection  
  34. $sth->finish();  
  35. $dbh->disconnect(); 

 

  Linux平台

  如果是在Linux平台下运行perl脚本,连接SQL Server数据库需要使用到DBD::Sybase包。

  安装SQL Server支持库

  Sybase DBD包依赖FreeTDS驱动程序。 FreeTDS下载地址:www.freetds.org 安装FreeTDS驱动的说明文档参见:http://www.freetds.org/userguide/config.htm 该驱动没有使用到ODBC.

  配置数据源

  修改freetds.conf文件包括SQL Server数据库信息,如下所示: [SS_MY_DB] host = 10.0.0.1 # or host name port = 1433 tds version = 7.0

  安装Sybase DBD模块

  该模块文档参见:http://search.cpan.org/~mewp/DBD-Sybase/Sybase.pm 此外,需要将sybase环境变量应设置为FreeTDS安装路径,export SYBASE=/usr/local/freetds

  使用Sybase DBI和SQL Server DSN实例

 

  1. load the DBI module  
  2. use DBI;  
  3. use DBD::Sybase;  
  4.   
  5. my $database="my_database";  
  6. my $user="sa";  
  7. my $auth="s3cr3t";  
  8.   
  9. BEGIN  
  10. {  
  11.  $ENV{SYBASE} = "/usr/local";  
  12. }  
  13.   
  14. Connect to the SQL Server Database  
  15. my $dbh = DBI->connect("dbi:Sybase:server=ss_my_db;database=$database",  
  16.  $user,  
  17.  $auth  
  18.  {RaiseError => 1, AutoCommit => 1}  
  19.  ) || die "Database connection not made: $DBI::errstr";  
  20.   
  21. #Prepare a SQL statement  
  22. my $sql = "SELECT id, name, phone_number FROM employees";  
  23. my $sth = $dbh->prepare( $sql );  
  24.   
  25. #Execute the statement  
  26. $sth->execute();  
  27.   
  28. my( $id, $name, $phone_number );  
  29.   
  30. # Bind the results to the local variables  
  31. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  32.   
  33. #Retrieve values from the result set  
  34. while( $sth->fetch() ) {  print "$name, $title, $phone\n";  
  35. }  
  36.   
  37. #Close the connection  
  38. $sth->finish();  
  39. undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase  
  40. $dbh->disconnect(); 

 

责任编辑:honglu 来源: 内存溢出
相关推荐

2010-07-15 17:28:50

SQL Server

2009-07-07 17:42:28

2011-08-09 09:31:39

SQL Server数connectionS

2009-08-03 14:17:18

C#连接AccessC#连接SQL Ser

2010-11-02 11:49:18

SQL SERVER连

2009-06-03 10:51:59

连接SQL数据库Adobe Dream

2009-11-12 11:23:35

ADO.NET SQL

2011-05-20 13:11:22

ADO.NET

2010-10-26 15:54:02

连接oracle数据库

2010-07-01 15:02:29

SQL Server数

2010-09-13 15:55:17

SQL Server数

2010-11-08 16:04:06

SQL SERVER连

2010-11-10 09:44:31

SQL Server端

2010-07-08 11:05:14

SQL Server数

2011-07-28 11:44:46

SQL Server数合并表格数据

2011-03-28 15:28:03

SQL Server 数据库

2009-04-22 09:42:07

SQL Server监视镜像

2021-05-17 06:57:34

SQLServer数据库

2010-06-18 10:20:22

SQL Server

2011-04-02 10:47:51

点赞
收藏

51CTO技术栈公众号