473,472 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to insert data into a table using perl ?

13 New Member
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 7532
RonB
589 Recognized Expert Moderator Contributor
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
omakhileshchand
13 New Member
thanks a lot RonB sir ...
Jun 19 '12 #3
nileshkirve
1 New Member
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.