in reply to Re: Re: Re: How hashes present keys and values.
in thread How hashes present keys and values.

OK, so, to do it right, I'd want :
sub create_SQLprepared{ my ($l_database, $l_tablename,@l_fields) = @_; my $l_fieldnames; my $l_placeholder; my $l_statement; my $l_fieldnames = join ",", (@l_fields); foreach (@l_fields) {$l_placeholder .= "?"} $l_placeholder = (join ",", (split //, $l_placeholder)); print "INSERT $l_tablename (" . $l_fieldnames . ")\n VALUES (" . $ +l_placeholder . ")"; $l_statement = $l_database->prepare("INSERT $l_tablename (" . $l_f +ieldnames . ")\n VALUES (" . $l_placeholder . ")"; return (\$l_prepare); }
? Where $l_database is a reference to a database handle, $l_tablename is a string, and @l_fields is a list of field names for the insert. Returns a reference to the statement handle.

Replies are listed 'Best First'.
Re: doing it the right way
by merlyn (Sage) on Dec 13, 2000 at 20:30 UTC
    Still seems far too clunky.
    sub create_SQLprepared { my ($db, $table, @fields) = @_; $db->prepare( "INSERT $table (". join(", ", @fields). ") VALUES (". join(", ", ("?") x @fields). ")" ); }

    -- Randal L. Schwartz, Perl hacker

Re: doing it the right way
by chipmunk (Parson) on Dec 13, 2000 at 20:29 UTC
    Yes, although a simpler way to generate $l_placeholder is:     my $l_placeholder = join ',', ('?') x @l_fields; ('?') x @l_fields creates a list of question marks, one for each element in @l_fields.

    (See my node on Variable placeholders with DBI.)