in reply to Re^2: Relational table with perl DBI
in thread Relational table with perl DBI
I tried using 'last_insert_id' and I get the id from my tables. The code looks like this :
#Insertion of extracted and synchronized data in 'article' table of e_ +slide database my $i_article; my $id_article; for ($i_article = 0; $i_article < @output_concord_files_prepare; $ +i_article++){ $dbh->do(" INSERT INTO `article`(`url`, `html_extr_text`,`concord_fil +e`, `sys_time`) VALUES ('$url_prepare[$i_article]', '$html_pages_files_ext +ended[$i_article]', '$output_concord_files_prepare[$i_article]', '$sy +s_time_prepare[$i_article]') ") || die $dbh->errstr; $id_article = $dbh->last_insert_id(undef, undef, 'article', 'i +d_article'); } #Insertion of extracted and synchonized data in 'event' table of e +_slide database my $i_event; my $id_event; for ($i_event = 0; $i_event < @event_prepare; $i_event++){ $dbh->do(" INSERT INTO `event`(`event`) VALUES ('$event_prepare[$i_event]') ") || die $dbh->errstr; $id_event = $dbh->last_insert_id(undef, undef, 'event', 'id_ev +ent'); }
So now, how do I get the one-to-many relationship in the third table? Because in this case one article contains multiple events. So the it would look like : 1 - 1, 1 - 2, 2 - 3, 2 - 4, 2 - 5 and so on.
I also manage to have a "good practice" code for the insertion using the code that you provided :
my @fields = (qw(url html_extr_text concord_file sys_time)); my $fieldlist = join ", ", @fields; my $field_placeholders = join ", ", map {'?'} @fields; my $insert_query = qq{ INSERT INTO article($fieldlist) VALUES ($field_placeholders) }; my $sth = $dbh->prepare($insert_query); foreach my $article_index (0 .. @output_concord_files_prepare){ $field_placeholders = $sth->execute($url_prepare[$article_inde +x], $html_pages_files_extended[$article_index], $output_concord_files +_prepare[$article_index], $sys_time_prepare[$article_index]); if ($field_placeholders != 1){ die "Error inserting records, only [$field_placeholders] g +ot inserted: " . $sth->insert->errstr; } }
But still I don't know how to make the one-to-manu relationship in perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Relational table with perl DBI
by Neighbour (Friar) on Mar 13, 2013 at 12:30 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 13:55 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 14:06 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 14:46 UTC | |
by Neighbour (Friar) on Mar 13, 2013 at 15:01 UTC | |
|