I am writing small snippets in PERL and DBI ( SQLite yay! )

Let me deviate a little bit and say how cool SQLite is. It makes handling a database just like string manipulation in PERL!

Anyways, what I wanted to know is that, I would like to log some specific queries to text files having the same filename as that of the table name(s) on which the query is run.

Here is the code I use to dump results to a text file :

sub dumpResultsToFile { my ( $query ) = @_; # Prepare and execute the query my $sth = $dbh->prepare( $query ); $sth->execute(); # Open the output file open FILE, ">results.txt" or die "Can't open results output file: +$!"; # Dump the formatted results to the file $sth->dump_results( 80, "\n", ", ", \*FILE ); # Close the output file close FILE or die "Error closing result file: $!\n"; }
Here is how I can call this :

dumpResultsToFile ( <<" END_SQL" ); SELECT TADA.fileName, TADA.labelName FROM TADA END_SQL

What I effectiively want is, instead of stuff going to "results.txt" ( that is hardcoded above ), it should now go to "TADA.txt".

Had this been a join between tables "HAI" and "LOL", then the resultset should be written to "HAI.LOL.txt"

Is what I am saying even possible using some magic in DBI?

I would rather do without parsing the SQL query for tables, but if there is a widely used and debugged function to grab source table names in a SQL query, that would work for me too!

What I want is just to have a filename that gives some hint as to what query output it holds. Segregating based on table name seems a nice way for now.


In reply to Grabbing source table names from a SQL query by PoorLuzer

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.