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.

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

In reply to Re: Data export into text file by gmax
in thread Data export into text file by b310

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.