use DBI; #connect and stuff. create the DB handle my %hash = ( 'col1'=>$somevalue, 'col2'=>$someothervalue, 'col3'=>$yav); my $sql = qq~INSERT INTO table1(~ . join(',', sort keys %hash). qq~) VALUES(~. join(',',(('?') x scalar( keys %hash))). qq~)~; my $sth = $dbh->prepare($sql); $sth->execute(@hash{(sort keys %hash)});