Setup a configuration file that contains all the valid field names for each database stored in hash or lists. This is some code I have been working on that may or may not help you, but has been very useful for my needs.
my %table_fields = ( table1 => [ 'field1' , 'field2' , 'field3' ], table2 => [ 'field1' , 'field2' , 'field3' ], ); sub get_fields { my ($self,$table) = @_; return $table_fields{$table}; }
Now you can pick through your current "keys" and see which ones match valid field names and pass only those to something like:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.