A working solution will probably end up looking something like this:
my @insert_cols = qw/BS queue_name in_q_per in_q_num out_q_per out_q_n +um/; my @inner_keys = @insert_cols[2..5]; my $dbh = DBI->connect( 'whatever...', 'name', 'pswd' ); my $insert_sth = $dbh->prepare( 'insert into my_table (' . join( ',', @insert_cols ) . ') values +(' . join( ',', ('?') x @insert_cols ) . ') +' ); for my $BS ( keys %HoH ) { for my $queue_name ( keys %{ $HoH{ $BS }} ) { my @nums = @{ $HoH{ $BS }{ $queue_name }} { @inner_keys }; $insert_sth->execute( $BS, $queue_name, @nums ); } }
(not tested, but it does compile)

However, if there's lot's of data to be inserted, you might want to consider just printing the rows to a tab-delimited text file, and use whatever tool your database server provides for doing bulk inserts from such a file. DBI tends to be very slow with inserts, compared to a compiled utility that is native to the DB server -- we're talking about differences of 10-to-1 or more in wall-clock time. (If it's not a lot of data, it doesn't matter, but when it gets into thousands of rows, you'll really notice the difference.)

Note that your perl script could both write the text file and then use "system()" to run the native bulk loader tool on that file, to keep the process fully integrated. It's a matter of replacing the $sth->execute(...) with print TSV join( "\t",...),"\n" and adding the necessary "open TSV, ...", "close TSV" and "system" calls around the for loop shown above.


In reply to Re: Hash of Hash to mysql by graff
in thread Hash of Hash to mysql by NothingInCommon

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.