sqlite - Can I get the table names from an SQL query with Perl's DBI? -


I'm writing short snippets in Perl and DBI (SQL Yay!)

I log in Want to put some specific questions in the name of the same filename after the name of the table name (s)?

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

sub dump reflections file {my ($ query) = @_; # Query prepare and execute my $ sth = $ dbh- & gt; Ready ($ query); $ Sth- & gt; Executed (); # Open the output file Open the file, "& gt; results.txt" or die "Result output file can not be opened: $!"; #Formatted results file $ sth- & gt; Dump into dump_reets (80, "\ n", ",", \ * FILE); # Close the output file FILE or die "Error result result file: $! \ N"; }

Here's how I can call it:

  dumpResultsToFile (& lt; & lt; "END_SQL");  

What I want effectively, instead of content that goes to "results.txt" (which is hard over), it should now go "TADTPTP".

If these tables have been added between "is" and "lo", the result should be written on "hah.loyl.txt"

am I DBi is saying possible even by using some magic?

I want to work without parsing the SQL query for the table, but if widely used and debugged function in the SQL query source < / Em> The table name, which will also work for me.

What I have to do, should be just a filename that gives some indication that it keeps the query output. Segregating by the name of the table is now a great way.

Maybe your SQL generation code takes a wrong view, you're hiding too much information from your program At the point, your program knows that instead of throwing that information out of which table to select and embeding it inside the opaque SQL command, you should put it around then your logger function does not need to estimate that log data Where should go; It knows .

Perhaps it is clear with some code. Your code looks like this:

  sub make_query {my ($ table, $ columns, $ condition) = @_; Select "$ table from $ column where $ terms" return; } Sub run_query {my ($ query) = @_; $ Dbh- & gt; ($ Query) ready; ...} run_query (make-quieri ('fu', '*', '1 = 1'));  

This does not let you do what you want to do. You should create your own program to do this:

  sub make_query {my ($ table, $ columns, $ terms) = @_; Return + {query = & gt; "$ Column FROM $ table from $ $ terms", table = & gt; $ Table,} # no object can not be a bad idea) sub-run_query {my ($ query) = @_; $ Dbh- & gt; Ready ($ query- & gt; {query}); Log_to_file ($ query- & gt; {tables}. 'Logs', ...); ...} run_query (make-quieri ('fu', '*', '1 = 1'));  

The API is the same, but the type of information you have now need to log on.

Also, consider for dynamic SQL generation. My code is just an example.

EDIT: OK, so you say that you are using SQLite. There is an expand command in it, which you can parse the output:

  sqlite & gt; * Select from the test, explain; 0 | Trace | 0 | 0 | 0 | Explain * Select from the test; | 00 | 1 | Goto | 0 | 11 | 0 || 00 | 2 | SetNumColumns | 0 | 2 | 0 || 00 | 3 | OpenRead | 0 | 2 | 0 || 00 | 4 | Rewind | 0 | 9 | 0 || 00 | 5 | Column | 0 | 0 | 1 || 00 | 6 | Column | 0 | 1 | 2 || 00 | 7 | ResultRow | 1 | 2 | 0 || 00 | 8 | Next | 0 | 5 | 0 || 00 | 9 | Off | 0 | 0 | 0 || 00 | 10 | Halt | 0 | 0 | 0 || 00 | 11 | Transactions | 0 | 0 | 0 || 00 | 12 | VerifyCookie | 0 | 1 | 0 || 00 | 13 | TableLock | 0 | 2 | 0 | Test | 00 | 14 | Goto | 0 | 2 | 0 || 00 |  

It seems that the table lock you want to find is that. YMMV, this is a bad idea.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -