sub InsertAndGetID { #--------------------------------------------------------------- # inserts an entry into a db and gets the auto_increment ID #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $Inserts = shift; my $IDCol = shift; $Inserts->{$IDCol} = 'NULL'; $dbh->do("LOCK TABLES $table WRITE") or die $dbh->errstr; InsertMultipleValues($dbh,$table,$Inserts); $sth = $dbh->prepare("SELECT LAST_INSERT_ID() FROM $table") or die $dbh->errstr; $sth->execute or die $dbh->errstr; my @ary = $sth->fetchrow_array or die $dbh->errstr; $dbh->do("UNLOCK TABLES") or die $dbh->errstr; $sth->finish; return $ary[0]; } sub InsertMultipleValues { #--------------------------------------------------------------- # Inserts contents of a hashref into the db table specified #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $Inserts = shift; my @cols = keys %$Inserts; my @vals = @$Inserts{@cols}; my $cols = join ',', @cols; my $places = '?,' x @vals; chop $places; my $sth = $dbh->prepare("INSERT INTO $table ($cols) VALUES ($places)") or die $dbh->errstr; $sth->execute(@vals) or die "$dbh->errstr : $table"; }