in reply to .ods file

This is my example program.. We have to export our .ods into .csv file. In this below example I have used that exported .csv file.

use strict; use warnings; use Data::Dumper; use DBI; # get the argument from (.csv file) from command line die "File not found\n" if(not defined(@ARGV)); my ($file)=@ARGV; my $flag=0; # connect the data base my $dbh=DBI->connect("DBI:Pg:dbname=sathishkumar","sathishkumar","s +athishkumar",{RaiseError=>1,AutoCommit=>1}); # using this function Check File already Available our database or +not my @Table_Name=$dbh->tables(); foreach(@Table_Name) { if($_ eq "dbi.config_table") { $flag=1; last; } } # Delete the previous row from this file $dbh->do("delete from dbi.config_table") if($flag==1); # Else create the table if($flag==0) { $dbh->do("create table dbi.config_table(id integer,process varc +har,ip varchar,port integer, deamon varchar,log_table varchar)"); } # prepare the Query to execute my $sh=$dbh->prepare("insert into dbi.config_table values(?,?,?,?,?, +?)"); # open the .csv file open FH,"<$file" or die "We can't open this file:$!"; while(<FH>) { chomp; my @line=split(/,/,$_); if($. != 1) { # remove all string delimiter $line[0] =~ s/\"//g; $line[1] =~ s/\"//g; $line[2] =~ s/\"//g; $line[3] =~ s/\"//g; $line[4] =~ s/\"//g; $line[5] =~ s/\"//g; my $id=$line[0]; my $process=$line[1]; my $Ip=$line[2]; my $port=$line[3]; my $Deamon=$line[4]; my $log_file=$line[5]; # execute the prepared Query $sh->execute($id,$process,$Ip,$port,$Deamon,$log_file); } } close FH; # disconnect the Data base. $dbh->disconnect();

Replies are listed 'Best First'.
Re^2: .ods file
by CountZero (Bishop) on Mar 14, 2009 at 11:37 UTC
    my @line=split(/,/,$_);
    See my comment above and repeat after me: I will never use a split on comma to deal with CSV-files.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James