1 package C4::Database; #asummes C4/Database
3 #requires DBI.pm to be installed
9 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
11 # set the version for version checking
15 @EXPORT = qw(&C4Connect &sqlinsert &sqlupdate &getmax &makelist
17 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
19 # your exported package globals go here,
20 # as well as any optionally exported functions
22 @EXPORT_OK = qw($Var1 %Hashit);
25 # non-exported package globals go here
26 use vars qw(@more $stuff);
28 # initalize package globals, first exported ones
33 # then the others (which are still accessible as $Some::Module::stuff)
37 # all file-scoped lexicals must be created before
38 # the functions below that use them.
40 # file-private lexicals go here
44 # here's a file-private function as a closure,
45 # callable as &$priv_func; it cannot be prototyped.
50 # make all your functions, whether exported or not;
56 # my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
57 my $database='c4test';
58 my $hostname='localhost';
61 my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
67 # my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
68 my $database='c4test';
69 my $hostname='localhost';
72 my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
79 my $query="INSERT INTO $table \(";
80 while (my ($key,$value) = each %data){
81 if ($key ne 'type' && $key ne 'updtype'){
82 $query=$query."$key,";
86 $query=$query." VALUES (";
87 while (my ($key,$value) = each %data){
88 if ($key ne 'type' && $key ne 'updtype'){
89 $query=$query."'$value',";
94 my $sth=$dbh->prepare($query);
101 my ($table,$keyfld,$keyval,%data)=@_;
103 my $query="UPDATE $table SET ";
105 my @keyarr = split("\t",$keyfld);
106 my @keyvalarr = split("\t",$keyval);
107 my $numkeys = @keyarr;
108 while (my ($key,$value) = each %data){
109 if (($key ne 'type')&&($key ne 'updtype')){
110 my $temp = " ".$key."='".$value."' ";
114 my $fsets = join(",", @sets);
115 $query=$query.$fsets." WHERE $keyarr[0] = '$keyvalarr[0]'";
118 while ($i < $numkeys) {
119 $query=$query." AND $keyarr[$i] = '$keyvalarr[$i]'";
123 # $query=~ s/\,$/\)/;
125 my $sth=$dbh->prepare($query);
133 my ($table,$item)=@_;
135 my $sth=$dbh->prepare("Select max($item) from $table");
137 my $data=$sth->fetchrow_hashref;
144 my ($table,$kfld,$dfld)=@_;
147 my $sth=$dbh->prepare("Select $kfld,$dfld from $table order by $dfld");
149 while (my $drec=$sth->fetchrow_hashref) {
150 $data = $data."\t".$drec->{$kfld}."\t".$drec->{$dfld};
156 END { } # module clean-up code here (global destructor)