padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

I have to convert data from a XBase to CSV,in these part I am OK
the problem starts when today they ask me to do it for several files
instead of only one file so for that reason I created a small CSV file to put the name of the client and the name of the file to work with here is the CSV file :
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
From this file I need to get de third column, thats the name of the file to put the data on,
so I connect to the CSV and read the whole file with a SELECT to get all the rows with data so I do it this way :
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) {
So I thought that with @$row[2] will do the trick but I was wrong
this is the whole program right up to now:
#!/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 mis +mo "; unlink ('c:/proyecto/@$row[2].csv'); } }
Where I got it wrong?
Thanks in Advance

Replies are listed 'Best First'.
Re: Doing something wrong
by ikegami (Patriarch) on Oct 19, 2007 at 17:53 UTC

    "but I was wrong" is a very poor description of the error you're getting.

    One thing you'll get is

    Can't declare array dereference in my

    my @$row is bad syntax. my $row.

    And to get the value of the second field for that row, $$row[2] or $row->[2]. Note the leading $, since a scalar is being returned.

      Hello!!!
      I did what you told me but now when
      I run the program it gives me these error message
      DBD::CSV::db do failed: Can't call method "fetchrow_array" on unblesse +d reference at C:/Perl/site/lib/SQL/Statement/Functions.pm line 560. Can't use an undefined value as an ARRAY reference at C:/Perl/site/lib +/SQL/Statement.pm line 264. [for Statement "SELECT * FROM IMPORT (?)"] at testdbf4.pl line 19. DBD::CSV::db do failed: Can't call method "fetchrow_array" on unblesse +d reference at C:/Perl/site/lib/SQL/Statement/Functions.pm line 560. Can't use an undefined value as an ARRAY reference at C:/Perl/site/lib +/SQL/Statement.pm line 264. [for Statement "SELECT * FROM IMPORT (?)"] at testdbf4.pl line 19.
      and this is the code with the modifications
      #!/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 mis +mo "; unlink ('c:/proyecto/$row->[2].csv'); } }
      Thank you for helping me !
      :o)
        Your errors now stem from the fact that you haven't successfully connected to any databases. (You could see that by appending || die "..." to your DBI->connect statements.)

        That is because you've not specified a database name (or host - I assume that's important for XBase) so the DBI drivers don't know where to look for the data.

        Please read the DBD::XBase and DBD::CSV documentation more carefully for connection string examples.

        -David