Greetings Monks,

I'm a high-beginner to intermediate at perl and DBI but this simple issue is eating my lunch.

I have a table of directory information that includes file sizes. I have been adding to the table, reading (select) from the table, etc. for months with no problems. Now I want to add up some selected file sizes. I keep hitting brick walls so I just ask about adding all the file sizes.

From the MySQL command prompt this works:
SELECT SUM(file_size) FROM dir_info;

But, using the same SQL statement inside perl returns a NULL.

With respect to my DB access I am patterning my code from the "MySQL and Perl for the Web" book by Paul DuBois (good book). At a high level I have:
* A DBI connect statement
* The SQL statement shown above stored in $SQLstr
* $sth = $dbh ->prepare($SQLstr);
* $sth -> execute();
* A while(@val = $sth->fetchrow_array()) loop

Many different SELECT statements work OK in my $SQLstr. Adding multiple "where this = that" statements connected by "AND" works OK. I can even use COUNT(*) with no problem. It seems odd to use fetchrow_array for a COUNT but I did not find another function and it worked so I was happy. But, no matter what I do I can't get SUM to work.

By bending the code around to get different error messages and Googling I have gotten hints of column vs. row operations and groupings. These ideas might be on the path to "truth" but it still seems that if the command line works then I should be able to do it in perl.

I have also gotten hints that I need to select in the SQL statement and then use SUM inside the while loop. This seems odd since a "SUM" inside the while loop would not be a SUM in a SQL statement.

Any suggestions or Wisdom?

Thanks,
Bruce

In reply to DBI SUM function by Bruce32903

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.