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...


In reply to Text::CSV not able to execute insert query dynamically. by Ankur_kuls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.