Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

DBD::CSV - How to insert a new column with column header in an existing CSV file

by slayedbylucifer (Scribe)
on Jul 19, 2012 at 11:59 UTC ( #982618=perlquestion: print w/ replies, xml ) Need Help??
slayedbylucifer has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to insert a new column along with the column header in an existing csv.

e.g. I have below csv:

vm_name, vm_cluster, vm1, ha1, vm2, ha2, vm3, ha3,

I am using DBD::CSV in my script to generate above mentioned file

Now, I want to add a 3rd column in above file to make it look like below:

vm_name, vm_cluster, Zone, vm1, ha1, zone2, vm2, ha2, zone3, vm3, ha3, zone1,

I tried doing below with the DBD::CSV but it did not work:

my $table = "mapping"; my $sth = $dbh->prepare ("INSERT INTO $table (Zone) VALUES (?,)" ); $sth->execute ( '<whatever_zone>');

but I get below error:

Execution ERROR: No such column 'Zone' called from dbd1.pl at 29

So I am thinking that DBD::CSV needs column name present already. So, I added the column name manually to the original csv file and ran above code and still it failed with same error.

Could you guide on how to get a column inserted in csv along with the column name. Also, just so you know, the values in this 3rd column will depend on some comparison of the respective values in column 1 and column 2 of the row. I'm hoping DBD::CSV should be able to do that because AFAIK Text::CSV does not have a mechanism to deal with column names.

Thanks.

Comment on DBD::CSV - How to insert a new column with column header in an existing CSV file
Select or Download Code
Re: DBD::CSV - How to insert a new column with column header in an existing CSV file
by Anonymous Monk on Jul 19, 2012 at 12:47 UTC
    Personally, I'd never have considered using an SQL 'INSERT' statement to modify a table's structure.

    Have you looked into the 'ALTER TABLE' or 'ALTER DATABASE' statements? A quick search leads me to: http://www.w3schools.com/sql/sql_alter.asp.

    TJD

Re: DBD::CSV - How to insert a new column with column header in an existing CSV file
by brx (Pilgrim) on Jul 19, 2012 at 13:54 UTC

    As I don't know SQL, my first idea was to deal with the csv as a text file. hmmm.

    It seems DBD::CSV doesn't like "ALTER TABLE" so, here is a POC with "CREATE TABLE".

    I hope, it is not the wrong way to do it.

    #!/usr/bin/env perl use strict; use warnings; use DBI; my $table = "t.csv"; my $newtable = "tnew.csv"; my $dbh = DBI->connect ("dbi:CSV:") or die "Cannot connect: $DBI::errstr"; my $query = "SELECT * FROM $table"; my $sth = $dbh->prepare ($query); $sth->execute (); unlink $newtable; $dbh->do ("CREATE TABLE $newtable (vm_name char(0), vm_cluster char(0) +, zone char(0))") or die "Cannot prepare: " . $dbh->errstr (); my ($vm_name, $vm_cluster); $sth->bind_columns (\$vm_name, \$vm_cluster,undef); while ($sth->fetch) { $dbh->do ("INSERT INTO $newtable VALUES (". $dbh->quote ($vm_name) . ",". $dbh->quote ($vm_cluster) . ",". $dbh->quote (&whatzone()) . ")" ); } $sth->finish; #unlink $table; #rename $newtable,$table; sub whatzone { return "zone".(1+int rand(3)); }
      brx, Thanks for the detailed response. your code structure gave me the direction to proceed. I have re-written your code and edited per my requirement but your concept remains the same. Thank you very much for your time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982618]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-09-17 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (89 votes), past polls