473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to insert data into a table using perl ?

Sir,

I have a text file that contain some fields,the fields are given below:-
"","7041","8320","hunt_incoming","7041","SIP/ccm102-00000001","","Read","REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5","2011-05-03 03:21:57","2011-05-03 03:21:59","2011-05-03 03:22:36",39,37,"ANSWERED","DOCUMENTATION","130439 2917.1",""

I want to insert that fields into a table using perl.I'm getting problem with particular fields "REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5" that fields break up into five part automatically,I want store that complete field into a specified column.

Code is given below:-


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use DBI;
  4.  
  5. print "Connecting to database...\n\n";
  6.  
  7. my $DSN = q/dbi:ODBC:SQLSERVER/;
  8. my $uid = q/migration/;
  9. my $pwd = q/mig0106@s/;
  10. my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";
  11. my $sth = undef;
  12.  
  13. my $cdr_log_file = "/tmp/Perl/test";
  14.  
  15. &dbformat if ($cdr_log_file eq "database_format");
  16. &usage if (!$pwd);
  17. &getNextField($_);
  18.  
  19. if ($dbh)
  20.                 {
  21.         print "Successfully connected to $DSN";
  22.         open cdr_log, "<$cdr_log_file" or die "Cannot open cdr_log_file\n";
  23.         while (<cdr_log>)
  24.                         {
  25.                  $_ =~ s/\n//iog;
  26.          $_ =~ s/\"//iog;
  27.  
  28.                 my (@fields) = split(/\,/, $_);
  29.  
  30.                 foreach (@fields) {
  31.                                  ($fields[0]   , $_) ;
  32.                                  ($fields[1]   , $_) ;
  33.                                  ($fields[2]   , $_) ;
  34.                                  ($fields[3]   , $_) ;
  35.                                  ($fields[4]   , $_) ;
  36.                                  ($fields[5]   , $_) ;
  37.                                  ($fields[6]   , $_) ;
  38.                                  ($fields[7]   , $_) ;
  39.                                  ($fields[8]  , $_); #= getNextField($_);
  40.                                  ($fields[9]   , $_) ;
  41.                                  ($fields[10]  , $_) ;
  42.                                  ($fields[11]  , $_) ;
  43.                                  ($fields[12]  , $_) ;
  44.                                  ($fields[13]  , $_) ;
  45.                                  ($fields[14]  , $_) ;
  46.                                  ($fields[15]  , $_) ;
  47.                                  ($fields[16]  , $_) ;
  48.                                  ($fields[17]  , $_) ;
  49.  
  50.                                  my $s = "insert into cdr (accountcode,src,dst,dcontext,clid, channel, dstchannel, lastapp, lastdata,start,answer,\[end\], duration, billsec, disposition, amaflags,userfield,uniqueid) values (\'".$fields[0]."\',\'".$fields[1]."\', \'".$fields[2]."\', \'".$fields[3]."\', \'".$fields[4]."\', \'".$fields[5]."\', \'".$fields[6]."\',\'".$fields[7]."\',\'".$fields[8]."\',\'".$fields[9]."\' ,\'".$fields[10]."\', \'".$fields[11]."\', \'".$fields[12]."\', \'".$fields[13]."\', \'".$fields[14]."\',\'".$fields[15]."\',\'".$fields[16]."\',\'".$fields[17]."\');\n";
  51.         $sth = $dbh->prepare($s);
  52.             $sth->execute();
  53.  
  54.                                  }
  55.  
  56. =cut
  57.                         sub getNextField {
  58.                                             my $s = shift;
  59.                                             my $delimiter = shift;
  60.                                             $delimiter or $delimiter = "\",\"";
  61.                                             my $endPos = index $s, "$delimiter";
  62.                                             $delimiter eq ","     and $endPos++;
  63.                                             $delimiter eq "\n"    and $endPos++;
  64.                                             $delimiter eq "\",\"" and $endPos+=2;
  65.                                             my $field = substr $s, 0, $endPos, "";
  66.                                             $field = substr $field, 0, -1;
  67.                                             (substr $field, -1) eq "\""   and $field = substr $field, 0 - 1;
  68.                                             (substr $field, 0, 1) eq "\"" and $field = substr $field, 1;
  69.                                             $field = $dbh->quote($field);
  70.                                             return $field, $s;
  71.                                         }
  72.  
  73. =cut
  74.                         }
  75.                 }
  76.  
  77. else    {
  78.                 die("Problem connecting to : $DSN\n");
  79.         }
  80.  
  81. print "\n\nEnd.\n";
  82. exit;
  83.  
  84.  
  85. sub usage()     {
  86.                 print_header();
  87.                 print "\nUsage:  <cdr_log_file> <sql_hostname> <Database> <Table> <Username> <Password>";
  88.                 print "\n\nTo see the Expected database format Database_format\n\nEnd.\n";
  89.                 die;
  90.                 };
  91.  
  92. sub dbformat()
  93.                 {
  94.                 print_header();
  95.                 print "\n Expects a table containing the following fields:\n
  96.         accountcode varchar(80) DEFAULT ''::character varying NOT NULL,
  97.         src varchar(80) DEFAULT ''::character varying NOT NULL,
  98.         dst varchar(80) DEFAULT ''::character varying NOT NULL,
  99.                 dcontext varchar(80) DEFAULT ''::character varying NOT NULL,
  100.         clid varchar(80) DEFAULT ''::character varying NOT NULL,
  101.                 channel varchar(80) DEFAULT ''::character varying NOT NULL,
  102.                 dstchannel varchar(80) DEFAULT ''::character varying NOT NULL,
  103.                 lastapp varchar(80) DEFAULT ''::character varying NOT NULL,
  104.                 lastdata varchar(80) DEFAULT ''::character varying NOT NULL,
  105.         start datetime with time zone DEFAULT now() NOT NULL, 
  106.         answer datetime with time zone DEFAULT now() NOT NULL,
  107.         [end] datetime with time zone DEFAULT now() NOT NULL,
  108.                 duration int DEFAULT (0)::bigint NOT NULL,
  109.                 billsec int DEFAULT (0)::bigint NOT NULL,
  110.                 disposition varchar(80) DEFAULT ''::character varying NOT NULL,
  111.                 amaflags varchar(80) DEFAULT ''::character varying NOT NULL,
  112.                 accountcode character varying(20) DEFAULT ''::character varying NOT NULL,
  113.         unquie character varying(255) DEFAULT ''::character varying NOT NUL,
  114.                 userfield character varying(255) DEFAULT ''::character varying NOT NULL
  115.                 ";
  116.                 die;
  117.                 };
  118.  
  119. sub print_header()
  120.                 {
  121.                 print "\nimport.pl - Load Asterisk CDR datas to SQL Server database\n";
  122.                 print "\n==============Storing data into SQL Server================\n";
  123.                 };
Please help me....
Jun 18 '12 #1

✓ answered by RonB

Your script has a number of problems, but to fix the one you're asking about you need to use the Text::CSV or Text::CSV_XS module. The XS module is faster, so that's what I'd use.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Text::CSV_XS;
  6. use Data::Dumper;
  7.  
  8. my $str = q("","7041","8320","hunt_incoming","7041","SIP/ccm102-00000001","","Read","REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5","2011-05-03 03:21:57","2011-05-03 03:21:59","2011-05-03 03:22:36",39,37,"ANSWERED","DOCUMENTATION","130439 2917.1","");
  9.  
  10. my $csv = Text::CSV_XS->new ( { binary => 1 } )
  11.         or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
  12.  
  13. $csv->parse($str);
  14. my @fields = $csv->fields;
  15.  
  16. print Dumper \@fields;
Outputs:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = [
  2.           '',
  3.           '7041',
  4.           '8320',
  5.           'hunt_incoming',
  6.           '7041',
  7.           'SIP/ccm102-00000001',
  8.           '',
  9.           'Read',
  10.           'REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5',
  11.           '2011-05-03 03:21:57',
  12.           '2011-05-03 03:21:59',
  13.           '2011-05-03 03:22:36',
  14.           '39',
  15.           '37',
  16.           'ANSWERED',
  17.           'DOCUMENTATION',
  18.           '130439 2917.1',
  19.           ''
  20.         ];

3 7525
RonB
589 Expert Mod 512MB
Your script has a number of problems, but to fix the one you're asking about you need to use the Text::CSV or Text::CSV_XS module. The XS module is faster, so that's what I'd use.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Text::CSV_XS;
  6. use Data::Dumper;
  7.  
  8. my $str = q("","7041","8320","hunt_incoming","7041","SIP/ccm102-00000001","","Read","REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5","2011-05-03 03:21:57","2011-05-03 03:21:59","2011-05-03 03:22:36",39,37,"ANSWERED","DOCUMENTATION","130439 2917.1","");
  9.  
  10. my $csv = Text::CSV_XS->new ( { binary => 1 } )
  11.         or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
  12.  
  13. $csv->parse($str);
  14. my @fields = $csv->fields;
  15.  
  16. print Dumper \@fields;
Outputs:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = [
  2.           '',
  3.           '7041',
  4.           '8320',
  5.           'hunt_incoming',
  6.           '7041',
  7.           'SIP/ccm102-00000001',
  8.           '',
  9.           'Read',
  10.           'REPFLOW,/usr/WINAST/PRAMERICA/ENGLISH/PREVIOUSMENU,1,,1,5',
  11.           '2011-05-03 03:21:57',
  12.           '2011-05-03 03:21:59',
  13.           '2011-05-03 03:22:36',
  14.           '39',
  15.           '37',
  16.           'ANSWERED',
  17.           'DOCUMENTATION',
  18.           '130439 2917.1',
  19.           ''
  20.         ];
Jun 18 '12 #2
thanks a lot RonB sir ...
Jun 19 '12 #3
I have same issue while importing data into the database. The issue was resolved by using Test::CSV module. Thank you so much Omakilesh & RonB.
Mar 23 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nel | last post by:
hi.. i want to know how to insert data using array in mysql where the data stored in field given? this field_name i taken from database.... thanks a lot...
1
by: nel | last post by:
actually i want to insert for example : "insert in $table_name1 f$ield_name1, $field_name2, $field_name3 values $data1, $data2, $data3"; table & field variable i get from select statement form...
3
by: jamesnkk | last post by:
I tried and search around the web, but could not find an answer to it, You see I am trying to insert 3 variables into the table, but seem like those " and ' cause the problem, No matter how I play...
9
by: pnsreee | last post by:
Hi All, Please help me to convert csv file to a hash table with out using Text::CSV_XS , Text::CSV. The CSV file is a dynamic file. the fields are like this Numb,Name,Class,Type,......
3
by: Indrachapa | last post by:
I want to get how to add data to table in SQL server database using a datatable using ASP.net
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
1
by: vinic | last post by:
This seems like it would be a straightforward function, but after searching all morning I'm a bit stuck. Into an exsiting db, I'd like to update a field (called "week") in rows with certain dates...
15
by: Maarten | last post by:
I've found some answers to my problem on this forum, but not exactly the answer I was looking for. Sorry if I've missed something. This is my situation: I am trying to make an insertion into an...
2
by: mahalakshmiDevi | last post by:
I am automating the testing of an web application. The application consist of HTML table with the list of items. I wanted to click on particular item of the table. I have provided the source code of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.