if (@{$ar_data}) { # If there is any data, prepare the query my @fields = keys %{$ar_data->[0]}; # Extract all the keys from the first element of ar_data my $fieldlist = join(',', @fields); my $field_placeholders = join(',', map {'?'} @fields); my $sth_insert_article = $dbh->prepare( qq( INSERT INTO article ($fieldlist) VALUES ($field_placeholders) )); my $sth_select_last_insert_id = $dbh->prepare( qq( SELECT LAST_INSERT_ID )); my $sth_insert_event = $dbh->prepare( qq( INSERT INTO event (event) VALUES (?) )); my $sth_insert_article_event = $dbh->prepare( qq( INSERT INTO article_event_index (id_article, id_event) VALUES (?,?) )); # Now we loop over and insert all data foreach my $hr_output (@{$ar_data}) { # Insert article data my $inserted_records = $sth_insert_article->execute(@{$hr_output}{@fields}); # Ok, I admit, this one is really tricky to read :) if ($inserted_records != 1) { die "Error inserting article [$hr_output->{url}], only [$inserted_records] got inserted: " . $sth_insert_article->errstr; } # Get article ID my $article_id = ($dbh->selectcol_arrayref($sth_select_last_insert_id))->[0]; # Insert events foreach my $event (@{$hr_output->{events}}) { $inserted_records = $sth_insert_event->execute($event); if ($inserted_records != 1) { die "Error inserting event [$event], only [$inserted_records] got inserted: " . $sth_insert_event->errstr; } # Get event ID my $event_id = ($dbh->selectcol_arrayref($sth_select_last_insert_id))->[0]; # Insert article_event combo $inserted_records = $sth_insert_article_event->execute($article_id, $event_id); if ($inserted_records != 1) { die "Error inserting article-event [$article_id, $event_id], only [$inserted_records] got inserted: " . $sth_insert_article_event->errstr; } } } }