<?xml version="1.0" encoding="windows-1252"?>
<node id="85626" title="Re: A more elegant solution?" created="2001-06-04 21:51:53" updated="2005-08-15 01:20:07">
<type id="11">
note</type>
<author id="31503">
runrig</author>
<data>
<field name="doctext">
Use placeholders. Substitute your hash keys in the SQL statement, but substitute the values in the execute. Generate the correct number of '?'s based on the number of fields in the hash.&lt;p&gt;
see [id://7548].&lt;p&gt;
Or just execute the statement in the sub.
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;Took out the map and put in the 'x' operator like [merlyn] has.&lt;code&gt;
sub insert_hash {
 my $table = shift;
 my $ref = shift;

 # Sort fields so we can cache the handle effectively
 my @fields = sort keys %$ref;
 my @values = @{$ref}{@fields};
 my $sql = "insert into $table (" . join(",", @fields).
  ") values (". join(",", ("?") x @fields).")";
 my $sth = $dbh-&gt;prepare_cached($sql);
 $sth-&gt;execute(@values);
}&lt;/code&gt;</field>
<field name="root_node">
85624</field>
<field name="parent_node">
85624</field>
</data>
</node>
