April 14, 2012
Perl DBI Connect Example
Here is a sample code snippet to connect and access an informix database using Perl DBI
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 34 35 36 37 38 39 40 |
#!/usr/bin/perl # DBI module use DBI ; # Database handler $dbh = DBI->connect("DBI:Informix:$db_name"); # Statement handler $sth = $dbh->prepare("$sql"); # Execute statement $sth->execute(); # For each row in the table while (@row = $sth->fetchrow_array()) { # Push the row into the output table push(@table, [@row]); } # Finish statement $sth->finish(); # Disconnect from database $dbh->disconnect(); # For each row for ($i = 0; $i <= $#table; $i++) { # For each column value for ($j = 0; $j <= $#{$table[$i]}; $j++) { # Print field value print "$table[$i][$j] "; } print "\\n"; } # if the statement is not a select u can use : $dbh->do($sql); |