服务器 频道

如何使用perl联结postgres数据库

  【IT168 服务器学院】下面是一个使用Pg类联结postgres的例子:

  #!/usr/bin/perl -w
  use strict;
  use Pg;
  my $pghost=''xxxx.xxxx.xxx'';
  my $pgport=''5433'';
  my $pgdatabase=''test'';
  my $pguser=''xxxxx'';
  my $pgpassword=''xxxxxxx'';
  my $conn=Pg::connectdb("pghost=$pghost pgport=$pgport pgdatabase=$pgdatabase pguser=$pguser pgpassword=$pgpassword") or die "could not connect ";
  if ($conn)
  {print "pgconnect ok! ";}
  else
  {print "pgconnect false! ";
  print "$conn->port(); ";
  }

  下面是一个使用DBI类联结postgres的例子:

  #!/usr/bin/perl -w

  use strict;

  use DBI;

  my $database_name = "test";

  my $location = "xxxxx.xxxxx.xxx";

  my $port_num = "5433";

  # 定义SQL服务器的位置.

  my $database ="dbi:Pg:dbname=$database_name;host=$location;port=$port_num";

  my $db_user = "xxxxx";

  my $db_password = "xxxxxxxx";

  ## 连接.

  my $dbh=DBI->connect($database,$db_user,$db_password) or die "can''t connect!";

  if ($dbh)

  {print "pgconnect ok! ";

  print "my $dbh->port(); ";

  }

  else

  {print "pgconnect false! ";

  print "my $dbh->port(); ";

  }

  #$dbh=DBI->disconnect;

   

0
相关文章