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

Hello Fellow monks need some help here. Below code works fine however I am facing other issues which are quite difficult to explain. basically whats happening is say my forename = Nitin then its correctly updating as Nitin, however if forename in workday file has 'Nitin Rane' my final output file has "Nitin Rane". I dont want " quotes in my final target final. is there any way to do it in existing code? otherwise I can simply loop these files and remove " but it may impact data as well if its valid " it shouldnt be removed? I am not sure where its adding " to the file may be DBI connect is taking csv_quote_char as " by default?
use DBI; require '_ENV.pl'; my $dbh = DBI->connect("dbi:CSV:f_dir=$_ENV::TempFilesDIR;csv_sep_char +=\\~") or die $DBI::errstr; $dbh->{'csv_tables'}->{'new_entrant'} = { 'file' => 'Aviva_cimp_ +new_entrant.txt', col_names => [qw(emp_no +cat_no forename surname initial ni_no ttl gender mrtl_sts + dob hmeadd1 hmeadd2 hmeadd3 hometown county homepost emp_cntr +btn emplyr_ctr anl_sal doj_schm dojcmpny exchanged schm_jnr) ] }; $dbh->{'csv_tables'}->{'workday'} = { 'file' => 'workday_mst +r_final.csv'}; my $query = "SELECT Employee_Number,Forename,Surname,Home_Address_Line +_1,Home_Address_Line_2,Home_Address_Line_3,Home_Town_City,Home_County +,Home_Post_Code FROM workday"; my $sth = $dbh->prepare ($query); $sth->execute(); $sth->bind_columns (\my ($wrk_emp_no,$wrk_forename,$wrk_surname,$wrk_h +me_add1,$wrk_hme_add2,$wrk_hme_add3,$wrk_hme_city,$wrk_hme_cnty,$wrk_ +postcode)); while (my $row = $sth->fetch) { $dbh->do('UPDATE new_entrant SET forename=?,surname=?,hmeadd1=?,hmeadd +2=?,hmeadd3=?,hometown=?,county=?,homepost=? WHERE emp_no=?',undef,$w +rk_forename,$wrk_surname,$wrk_hme_add1,$wrk_hme_add2,$wrk_hme_add3,$w +rk_hme_city,$wrk_hme_cnty,$wrk_postcode,$wrk_emp_no) or die $DBI::err +str; } $sth->finish(); $dbh->disconnect();

Replies are listed 'Best First'.
Re: CSV Update Issues
by Tux (Canon) on May 12, 2015 at 10:51 UTC

    PLEASE rewrite to using hash-attributes connect instead of DSN!

    new scroll down to quote_space

    quote_space

    my $csv = Text::CSV_XS->new ({ quote_space => 1 }); $csv->quote_space (0); my $f = $csv->quote_space;

    By default, a space in a field would trigger quotation. As no rule exists this to be forced in CSV, nor any for the opposite, the default is true for safety. You can exclude the space from this trigger by setting this attribute to 0.

    Your connect will need to add

    my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { # ...... csv_quote_space => 0, });

    Enjoy, Have FUN! H.Merijn
Re: CSV Update Issues
by hippo (Archbishop) on May 12, 2015 at 10:50 UTC
    I am not sure where its adding " to the file may be DBI connect is taking csv_quote_char as " by default?

    Yes, it is as described in the documentation. Change the csv_quote_char and the problem should be resolved.

      That is NOT what the OP asked for!


      Enjoy, Have FUN! H.Merijn