MySQL Forums
Forum List  »  Perl

Re: How to save a column of db to txt file?
Posted by: Bill Karwin
Date: May 25, 2006 10:58AM

You can use the INTO OUTFILE syntax in any SELECT query:

SELECT columnname
INTO OUTFILE 'output.txt'
FROM tablename

See http://dev.mysql.com/doc/refman/5.0/en/select.html

Note that this creates a file on the host where MySQL Server is running, which is not necessarily the same host where your Perl script is running.

For what it's worth, you don't necessarily have to load all the data into memory when querying from Perl. You can use DBI to execute a query, and then loop over the rows of the result set. Your script would only store one row of the result set at any given time, during this loop. It would write the data to a file as it goes.

For example:

$sth = $dbh->prepare("SELECT foo FROM table");
$sth->execute();
open(TEXTFILE, ">output.txt") or die($!);
while (@row = $sth->fetchrow_array()) {
print TEXTFILE "@row\n";
}
close(TEXTFILE);

Regards,
Bill K.



Edited 1 time(s). Last edit at 05/25/2006 11:04AM by Bill Karwin.

Options: ReplyQuote


Subject
Written By
Posted
Re: How to save a column of db to txt file?
May 25, 2006 10:58AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.