I tested this on a dir with 3 files and it is not inserting the
data into the table fields. The tables are related via the $id(pk in blocks and fk in vials).<br
<readmore><c> #!/usr/bin/perl -w use strict; use warnings; use DBI; ################################################ # Load the files into the MySQL database # ################################################ my $dbh = DBI->connect("DBI:mysql:dev:informatics", "mystuff", "gene77 +7" ) || die "Could not connect to database:\$DBI::errstr"; my $sth1 = $dbh->prepare("INSERT INTO dev.blocks (names)VALUES(?)"); + my $sth2 = $dbh->prepare("SELECT id from dev.blocks where name = ?"); my $sth3 = $dbh->prepare("INSERT INTO dev.vials (block_id, lt, well_po +sition, gene, barcode)VALUES(?,?,?,?,?)"); # Put all of the files into an array for loading my $count = 0; # loop through the filehandles.treat $file as a normal # file handle my @files my @files; foreach my $file(@files) { open(my $file, "/home/mydir/data/test_files/$file"); while(<$file>) { chomp; my @fields = split /\t/; my $well = $fields[0]; my $gene = $fields[1]; my $lt = $fields[2]; my $barcode = $fields[3]; my $name = $fields[4]; # Insert data into tables # $name indices file are to be loaded into the 'blocks' table +'name' field $sth1->execute($name); # query dev.blocks for id by passing in the $name $sth2->execute($name); # get the result from the blocks table and assign it to a vari +able. my @result = $sth2->fetchrow(); my $id = $result[0]; # Insert the variable into the foreign key block_id in the via +ls table along # with the other variables to be loaded into the vials table. $sth3->execute($id,$lt, $well, $gene, $barcode); } close($ltvec_file); $count++; }
Please excuse the previous typo.

In reply to Re^2: Is there a more efficient way? by lomSpace
in thread Is there a more efficient way? by lomSpace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.