in reply to Data export into text file

If you want to store your data somewhere, rather than sending it directly to the other DBMS, here's a general purpose dump script.

It exports data in SQL format, so that it could be later given to any DBMS that supports ANSI SQL standards.

#!/usr/bin/perl -w # dump.pl # simple SQL table dump use strict; use DBI; my ($db, $table) = @ARGV; $table or die "syntax: dump database table\n"; my $driver = 'Your DBD driver name here'; my $dbh = DBI->connect("dbi:$driver:$db", 'myusername', 'mypassword', {RaiseError=>1}) or die "Can't open: $DBI::errstr\n"; my $sth= $dbh->prepare(qq{select * from $table}); $sth->execute(); my $fields = join "," , @{$sth->{NAME}}; my $insert_statement = qq{INSERT INTO $table ($fields)\n VALUES (}; while (my $row = $sth->fetchrow_arrayref()) { print $insert_statement, join( ",", map( {$dbh->quote($_)} @$row)), ");\n"; } $dbh->disconnect; __END__ sample output using this "department" table +-----------+-------------+------------+ | dept_code | dept | sort_order | +-----------+-------------+------------+ | 1 | Personnel | 5 | | 2 | Training | 2 | | 3 | Research | 4 | | 4 | Sales | 1 | | 5 | Development | 3 | +-----------+-------------+------------+ INSERT INTO department (dept_code,dept,sort_order) VALUES ('1','Personnel','5'); INSERT INTO department (dept_code,dept,sort_order) VALUES ('2','Training','2'); INSERT INTO department (dept_code,dept,sort_order) VALUES ('3','Research','4'); INSERT INTO department (dept_code,dept,sort_order) VALUES ('4','Sales','1'); INSERT INTO department (dept_code,dept,sort_order) VALUES ('5','Development','3');
Should you need to export into CSV, replace from my fields to the end of the loop with this crude snippet

while (my $row = $sth->fetchrow_arrayref()) { print join( ",", map( {"\"$_\""} @$row)), "\n"; }

To create a tab separated file, use

print join("\t", @$row),"\n";

You get the idea.

The script is rather simple, but you could easily customize it to suit your exporting needs.
For example, to quote the values for a specific DBMS, you may connect to it and use its DB handler "quote" method.

_ _ _ _ (_|| | |(_|>< _|

Replies are listed 'Best First'.
Re: Re: Data export into text file
by b310 (Scribe) on Mar 17, 2003 at 19:21 UTC
    Hi Gmax,

    Thanks for the dump script. I'll play with it and see what I get.
Re: Re: Data export into text file
by b310 (Scribe) on Mar 18, 2003 at 16:15 UTC
    Hi Gmax,

    I tried your general dump of my data and I'm receiving an Internal Server Error.