M15U has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks!!! I have a question for you.
I'm using DBI module in perl and I have a problem that I cannot wrap my mind around it. Lets say I have this table called 'article', which I fill with some data collected from different files, the code looks like this :
my $id_article = 0; for ($i_article = 0; $i_article < @output_concord_files_prepare; $ +i_article++){ $dbh->do(" INSERT INTO `article`(`id_article`, `url`, `html_extr_text +`,`concord_file`, `sys_time`) VALUES ('$id_article', '$url_prepare[$i_article]', '$html_ +pages_files_extended[$i_article]', '$output_concord_files_prepare[$i_ +article]', '$sys_time_prepare[$i_article]') ") || die $dbh->errstr; } $id_article++;
The code works. Each array contains strings of characters which are inserted in the 'article' table.
Now I have another table called 'event' :
my $id_event = 0; for ($i_event = 0; $i_event < @event_prepare; $i_event++){ $dbh->do(" INSERT INTO `event`(`id_event`, `event`) VALUES ('$id_event', '$event_prepare[$i_event]') ") || die $dbh->errstr; } $id_event++;
The thing is now that one article contains multiple events. So I create a third table 'article_event_index' which looks like this :
$create_query = qq{ create table article_event_index( id_article int(10) NOT NULL, id_event int(10) NOT NULL, primary key (id_article, id_event), foreign key (id_article) references article (id_article), foreign key (id_event) references event (id_event) ) }; $dbh->do($create_query);
In the collection data part of my code I have all the references that I need :
#!/usr/bin/perl -w use strict; use locale; use warnings; #use diagnostics; use utf8; binmode(STDIN, "encoding(utf8)"); binmode(STDOUT, "encoding(utf8)"); binmode(STDERR, "encoding(utf8)"); #Directory with Unitex output files my @output_concord_files = glob("output_concord/*.txt"); #Using 'glob' implies random order of files => sort @output_concord_files = map{$_->[1]} sort{$a->[0] <=> $b->[0]} map +{/output_concord\/concord\.(.*)\.txt/; [$1, $_]} @output_concord_file +s; my $index_file = "index.txt"; open (INDEX, '>:utf8', $index_file) || die "Couldn't open $index_f +ile : $!\n"; my $event; foreach my $output_concord_file(@output_concord_files){ open (my $fh, '<:utf8', $output_concord_file) || die "Couldn't + open $output_concord_file : $!\n"; while (<$fh>){ if ($_ =~ /=E-(.*)=event/){ $event = $1; print "$output_concord_file -> $event\n"; print INDEX "$output_concord_file -> $event\n"; } } }
The output would be : outputcondord.0.txt -> rockfall outputcondord.0.txt -> avalanche outputcondord.1.txt -> rockfall outputcondord.2.txt -> rockfall And so on...
Now, I don't know how to make the perl statement which will fill the 'article_event_index' table. I use a 'for' loop to populate the two others tables and I increment the id for each one of them. Is this good practice ? What is the "good practice" for this kind of operation ? I search for days on the web one example which will simulate what I want here, but I didn't found anything. I'm open also to the 'prepare - execute' DBI method in contrast to 'do', it's the same thing for me because the computing time is not that important in this task. Hope that I was clear enough. Thank you Monks !!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Relational table with perl DBI
by Neighbour (Friar) on Mar 13, 2013 at 08:49 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 09:01 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 10:13 UTC | |
by Neighbour (Friar) on Mar 13, 2013 at 12:30 UTC | |
by M15U (Acolyte) on Mar 13, 2013 at 13:55 UTC | |
| |
by poj (Abbot) on Mar 14, 2013 at 10:43 UTC |