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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#!C:\Perl\bin\perl.exe
use strict;
use Data::Dumper;
use DBI;
use vars qw/
$hostPostgres $portPostgres $userPostgres $passPostgres $database @tables $dbh @tables
/;
$hostPostgres='localhost';
$portPostgres='5432';
$userPostgres='postgres';
$passPostgres='myPass';
$database='ConfigMGT';
@tables=({'name'=>'Test','attribs'=>['id','name','param1','param2'],'nb'=>4},
);
&connectDB();
&executeSql("INSERT INTO Test(id,name,param1,param2) VALUES (0,'nom1','seul param',)");
my $cmd=&executeSql("SELECT DISTINCT * FROM Test ORDER BY name");
while ( my @lines =$cmd -> fetchrow_array) {
my $out="(";
foreach (@lines){
$out.="$_,";
}
chop($out);
$out.=")\n";
print $out;
}
&disconnectDB();
exit 0;
sub connectDB(){
unless(defined $dbh) {
$dbh = DBI->connect(
"dbi:PgPP:dbname=$database;host=$hostPostgres;port=$portPostgres",
$userPostgres, $passPostgres
);
warn "dbh is defined now :p\n";
}
else{
warn "dbh already defined !\n";
}
}
sub executeSql(){
my $req=shift;
&connectDB();
my $cur=$dbh->prepare($req);
$cur->execute();
die "Prepare Error: $DBI::errstr" if $DBI::err;
return $cur;
}
sub disconnectDB(){
$dbh->disconnect() if (defined($dbh));
} |
Partager