domaniqs:

I'm guessing that some of your data has embedded quotes and/or commas in it. That has an opportunity to confuse the CSV handler *and* your database handling code. In this case, it appears to be your database handling code.

The difficulty is getting your data properly quoted for use with the database. Luckily, it's such a common difficulty that DBI has support for it built in: Placeholders. Rather than building the value strings into your SQL, you use a question mark as a placeholder. Then, when you execute the statement, you provide an array containing the values for the question marks, in order. So I'd suggest modifying your program to something like:

my $SQL = "INSERT INTO $table VALUES (" . join(",", map { "?" } $csv->fields) . ")"; $prep = $db->prepare($SQL) or die "..."; while (my $row = $csv->getline($fh)) { $prep->execute($csv->fields); }

The first bit build a string with a set of ? placeholders for the fields. It does so by using the map operator to convert the list of fields into a list of question marks, and join to add the commas between then.

Then we prepare the statement for execution. Notice one thing: The prepare statement is *outside* your loop. We can do this because the statement doesn't change for each iteration!

So if your database drivers support precompiling statements (and most do), then you get a speed boost because it only compiles the statement one time! Finally, in the loop we simply execute the statement with a different list of values each time.

Please learn to use placeholders, and you'll find a lot of database work simpler, safer and more reliable.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Use of uninitialized value in join or string by roboticus
in thread Use of uninitialized value in join or string by domaniqs

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.