#!/usr/bin/perl -w use DBI; use strict; my $dbh = DBI->connect("DBI:mysql:test;host=localhost" . ";mysql_read_default_file=$ENV{HOME}/.my.cnf", undef, undef, {RaiseError => 1}) or die "can't connect\n"; my $pairs = { ONE => 'RTEST', TWO => undef, THREE => 'ANOTHER' }; # Creating a dummy table to match the example $dbh->do(qq{CREATE TABLE IF NOT EXISTS web_sessions_aux (wsa_id INT not null, wsa_type char(10), wsa_key char(10), wsa_value char(10))}); my ($id,$type) = (1,'UNKNOWN'); my $sth = $dbh->prepare(" INSERT INTO web_sessions_aux (wsa_id, wsa_type, wsa_key, wsa_value) VALUES (?,?,?,?)"); DBI->trace(2); foreach my $key (keys %$pairs) { my $key_value = defined($pairs->{$key})? $pairs->{$key} : ''; $sth->execute($id,$type,$key,$key_value); } DBI->trace(0); $dbh->disconnect();