2009-05-22 56 views

回答

4

您將需要使用DBI,你可能是最好的使用來自(CPAN)的DBD :: ODBC提供程序。如果您不瞭解DBI,那麼您需要閱讀相關內容。有一本書(Programming the Perl DBI)這是舊的,但仍然有效。

然後像下面這樣:

use strict; 
use warnings; 
use DBI; 

# Insert your DSN's name here. 
my $dsn = 'DSN NAME HERE' 

# Change username and password to something more meaningful 
my $dbh = DBI->connect("DBI:ODBC:$dsn", 'username', 'password') 

# Prepare your sql statement (perldoc DBI for much more info). 
my $sth = $dbh->prepare('select id, name from mytable'); 

# Execute the statement. 
if ($sth->execute) 
{ 
    # This will keep returning until you run out of rows. 
    while (my $row = $sth->fetchrow_hashref) 
    { 
     print "ID = $row->{id}, Name = $row->{name}\n"; 
    } 
} 

# Done. Close the connection. 
$dbh->disconnect; 
2

下面是使用DBI一個基本的例子(評論後可進行編輯):

use DBI; 

my $dbh = DBI->connect("dbi:Sybase:database=<dbname>;server=<servername>", 
    <user>, <password>, 
    { PrintError => 0, RaiseError => 1 }); 
my $sth = $dbh->prepare("select field from table"); 
my $result = $sth->execute(); 
while(my $result = $sth->fetchrow_hashref) { 
    print $result->{field}; 
} 
$sth->finish; 
$dbh->disconnect; 

希望能看到其他的答案有一個簡單的解決方案:)

+1

你不一般需要指定dbi_connect_method。我建議使用「PrintError => 0,RaiseError => 1」,以便引發SQL錯誤......特別是因爲您不做任何錯誤檢查;) 您甚至可以通過簡單地說「for my $ row in(@ {$ dbh-> selectall_arrayref(「select table from table」,{Slice => {}})}){print「$ row - > {field} \ n」;} 如果你是打算使用fetchrow_hashref或其朋友,值得設置「FetchHashKeyName =>'NAME_lc'」,當連接到DB時,有些人堅持使用CamelCase作爲列名 但除了這一切,罰款:) – araqnid 2009-05-22 13:02:10

+0

好的建議,編輯 – Andomar 2009-05-22 14:36:47

0
 # 
     # ------------------------------------------------------ 
     # run a passed sql and retun a hash ref of hash refs 
     # ------------------------------------------------------ 
     sub doRunSqlGetHashRef { 

      my $self     = shift ; 
      my $sql     = shift ; 

      my $hsr_meta    = {} ; 
      my $hsr     = {} ; 
      my $rowid = 0 ; 
      my $flag_filled_hsr_meta = 0 ; 
      my $hsr_meta_colid   = 0 ; 

      use DBI; 

      my $dbs = "dbi:ODBC:DRIVER=FreeTDS;DSN=DEV_MSSQLSRV_DSN"; 
      # verify by : 
      # isql -v DEV_MSSQLSRV_DSN user pwd 

      my $dbh = DBI->connect($dbs, $db_user, $db_user_pw) 
       or die "CONNECT ERROR! :: $DBI::err $DBI::errstr $DBI::state $!\n"; 

      if (defined($dbh)) { 

       # Prepare your sql statement (perldoc DBI for much more info). 
       my $sth = $dbh->prepare($sql) ; 

       # Execute the statement. 
       if ($sth->execute) { 
       # This will keep returning until you run out of rows. 
        while (my $row = $sth->fetchrow_hashref) { 

         # fill in the meta hash reference with the col names 
         if ($flag_filled_hsr_meta == 0) { 
          for (@{$sth->{ 'NAME' }}) { 
           # debug ok print "$_ => $row->{$_}\t"; 
           $hsr_meta->{ $hsr_meta_colid } = $_ ; 
           $hsr_meta_colid++ ; 
           $flag_filled_hsr_meta = 1 ; 
          } 
         } 
         # p ($row) ; # row level debug ... 
         $hsr->{ $rowid } = $row ; 
         $rowid++ ; 
        } 
       } 
       # Done. Close the connection. 
       $dbh->disconnect; 

       # debug ok p($hsr_meta) ; 

       return ($hsr_meta , $hsr) ; 
      } 
      else { 
       print "Error connecting to database: Error $DBI::err - $DBI::errstr\n"; 
      } 

     } 
     #eof sub doRunSqlGetHashRef