patentes,nombre,archivo,email 1458,juan armando,1458DATA,ao@gmail.com 2500,Armando Guajardo,2500DATA,a8@prodigy.net.mx 3500,AAA,3500DATA,adez@aa.com 3700,Armando Juan ,3700DATA,armo@hotmail.com 4500,padawan,4500DATA,padawan@gmail.com #### my $dbh = DBI->connect("DBI:CSV:f_dir=/Proyecto/") or die "Cannot connect: " . $DBI::errstr; my $sth = $dbh->prepare("SELECT * from patentes.csv") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while ( my @$row = $sth->fetch) { #### #!/usr/bin/perl use warnings; use strict; use DBI; #---- This part conects to my 2 databases one CSV and one XBase ----- my $dbhX = DBI->connect('dbi:XBase(RaiseError=1):'); my $dbhC = DBI->connect('dbi:CSV(RaiseError=1):'); my $select = $dbhX->prepare("SELECT * FROM reg501"); $select->execute(); #--- This one to the table on CSV where I have the data ------ my $dbh = DBI->connect("DBI:CSV:f_dir=/Proyecto/") or die "Cannot connect: " . $DBI::errstr; my $sth = $dbh->prepare("SELECT * from patentes.csv") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while ( my @$row = $sth->fetch) { $dbhC->do("CREATE TABLE @$row[2].csv AS IMPORT(?)",{},$select); my $mysql_dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root", "xyz123", {'RaiseError' => 1}); #---- Here I empty the table in MySql ------ $mysql_dbh->do("TRUNCATE TABLE @$row[2]"); #---- Here I load all the data ------- my $sql = "LOAD DATA LOCAL INFILE 'c:/proyecto/@$row[2].csv' INTO TABLE @$row[2] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'"; my $sth = $mysql_dbh->prepare($sql); $sth->execute (); if (-e "c:/proyecto/@$row[2].csv") { print "Advertencia el archivo existe iniciando el borrando del mismo "; unlink ('c:/proyecto/@$row[2].csv'); } }