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

I have an ods file which contains the data to insert into the database. How can I achieve this using perl?

Replies are listed 'Best First'.
Re: .ods file
by CountZero (Bishop) on Mar 14, 2009 at 08:21 UTC
    What kind of .ods are you talking about: a Microsoft Outlook 5 e-mail message store or an OpenOffice spreadsheet file?

    A general procedure (applicable to all kinds of files) could be:

    • Open the input file on a record-by-record basis.
    • Split into individual fields.
    • Reformat, change, combine, ... these fields as necessary.
    • Open a connection to your database.
    • Assemble the fields into a record.
    • Save the record to the database.

    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

Re: .ods file
by targetsmart (Curate) on Mar 14, 2009 at 08:33 UTC
    Save/Export As CSV and you have numerous CSV modules in perl to parse it(use the link below), use DBI and DBD modules to insert the parsed data into the database.

    http://search.cpan.org/search?query=csv&mode=all


    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: .ods file
by leslie (Pilgrim) on Mar 14, 2009 at 10:53 UTC

    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();
      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

Re: .ods file
by vinoth.ree (Monsignor) on Mar 14, 2009 at 08:49 UTC

    If your '.ods' file is a spreadsheet export this file to '.csv' file and open that file and use spite function and get data

    Try the following code for simple .ods file, your .ods file content will be in the "@arr" use it in the query.

    open(FILEHAND,"ex1.csv") || die "File can't be open\n"; my $f_line=<FILEHAND>; $f_line=~ s/\"//g; my @arr=split /\,/ ,$f_line;
      This generally considered not the way to deal with CSV-files.

      What will happen if your file containes embedded delimiters? Through $f_line=~ s/\"//g; you have destroyed the quotes (why?) and then you naively split on the comma (which BTW does not have to be escaped in your regex). If you have embedded comma's in your fields you are now totally lost.

      Use any of the CSV modules, such as Text::CSV to do this "The Right Way".

      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