Hi, I have the below script which reads one csv file in a directory, connect to the SQLite database, and insert the data of csv files into the database tables..
#! /usr/bin/perl use warnings; use strict; use DBI; use Text::CSV; use Data::Dumper; our $FH; open ($FH, "abc.csv") or die "can't open the fileo $!"; my $csv = Text::CSV->new({ sep_char => ";", binary => 1, auto_diag => 1 }); my $dbh; $dbh = DBI->connect('DBI:SQLite:vodafone.db') || die "$DBI::errstr\n"; my $columns = $csv->getline ($FH); # skip header my $count = $#{$columns}; my @array5; for (0..$count) { $array5[$_] = "'\$row->[$_]'"; } my $query = join (", ", @array5); print $query."\n"; while (my $row = $csv->getline($FH) ) { #$dbh->do("insert into VoucherMRPDataTable_Frt_TariffModel values ('$r +ow->[0]', '$row->[1]', '$row->[2]', '$row->[3]');"); $dbh->do("insert into VoucherMRPDataTable_Frt_TariffModel values ($que +ry);"); } close($FH); $dbh->disconnect;
why i am using $query because I don't know how many columns this CSV file contains... Now when I execute the first insert query (commented out) I am getting the desired result ie field values of csv file. but when I am using the second insert query (which is my requirement) it is storing $row->[0], $row->1 etc values in database...please help...
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |