Now you can pick through your current "keys" and see which ones match valid field names and pass only those to something like:my %table_fields = ( table1 => [ 'field1' , 'field2' , 'field3' ], table2 => [ 'field1' , 'field2' , 'field3' ], ); sub get_fields { my ($self,$table) = @_; return $table_fields{$table}; }
sub save { my $self = shift; my $table = shift; my $args = shift; # this is your hash of fields and values my @place = (); my @field = (); my @value = (); # # insert into table # foreach (keys %{$args}) { push @place, '?'; push @field, $_; push @value, $args->{$_}; } my $string = qq[ insert into $table ( ] . join(' ,', @field) . qq[ ) values ( ] . join(' ,', @place) . qq[ ) ]; $self->error_to_log("$string"); my $id = $self->db_do($string , \@value ); return ($id); } sub db_do { my $self = shift; my $string = shift; my $placeholders = shift; my $id; $self->error_to_log("$string"); my $cursor = $self->dbh->prepare($string); $cursor->execute(@{ $placeholders }); if ($string =~ /^\s?insert/i) { $id = $self->dbh->{'mysql_insertid'}; $self->error_to_log("ID $id",1); } return ( $id ); }
In reply to Re: Creating a Flexible Database Module
by trs80
in thread Creating a Flexible Database Module
by gwhite
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |