#!/usr/bin/perl

use warnings;
use strict;
use DBI;

my($host, $base, $usuario, $clave, $tabla) =
	qw/localhost test apache telecom9 tabla1/;
my($dircsv, $archcsv) = ("./", "datos_de_mysql");

my($dbh1, $sth1, $dbh2, $sth2, @fila, @columnas);

$dbh1 = DBI->connect("dbi:mysql:host=$host;database=$base", $usuario, $clave,
	{ RaiseError => 1, AutoCommit => 1 });

# DESCRIBE devuelve Field, Type, Null, Key, Default, Extra
$sth1 = $dbh1->prepare("DESCRIBE $tabla");
$sth1->execute();
while(@fila = $sth1->fetchrow_array()) {
	push(@columnas, $fila[0]);
}

$dbh2 = DBI->connect("dbi:CSV:f_dir=$dircsv", "", "",
	{ RaiseError => 1, AutoCommit => 1 });

$dbh2->do("CREATE TABLE $archcsv (" . join(", ", map("$columnas[$_] varchar(255)", 0..$#columnas)) . ")");

$sth2 = $dbh2->prepare("INSERT INTO $archcsv (" . join(', ', @columnas) .
	") VALUES (" . join(", ", map("?", @columnas)) . ")");

$sth1 = $dbh1->prepare("SELECT * FROM $tabla");
$sth1->execute();
while(@fila = $sth1->fetchrow_array()) {
	$sth2->execute(@fila);
}

