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:- - #!/usr/bin/perl
-
-
use DBI;
-
-
print "Connecting to database...\n\n";
-
-
my $DSN = q/dbi:ODBC:SQLSERVER/;
-
my $uid = q/migration/;
-
my $pwd = q/mig0106@s/;
-
my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";
-
my $sth = undef;
-
-
my $cdr_log_file = "/tmp/Perl/test";
-
-
&dbformat if ($cdr_log_file eq "database_format");
-
&usage if (!$pwd);
-
&getNextField($_);
-
-
if ($dbh)
-
{
-
print "Successfully connected to $DSN";
-
open cdr_log, "<$cdr_log_file" or die "Cannot open cdr_log_file\n";
-
while (<cdr_log>)
-
{
-
$_ =~ s/\n//iog;
-
$_ =~ s/\"//iog;
-
-
my (@fields) = split(/\,/, $_);
-
-
foreach (@fields) {
-
($fields[0] , $_) ;
-
($fields[1] , $_) ;
-
($fields[2] , $_) ;
-
($fields[3] , $_) ;
-
($fields[4] , $_) ;
-
($fields[5] , $_) ;
-
($fields[6] , $_) ;
-
($fields[7] , $_) ;
-
($fields[8] , $_); #= getNextField($_);
-
($fields[9] , $_) ;
-
($fields[10] , $_) ;
-
($fields[11] , $_) ;
-
($fields[12] , $_) ;
-
($fields[13] , $_) ;
-
($fields[14] , $_) ;
-
($fields[15] , $_) ;
-
($fields[16] , $_) ;
-
($fields[17] , $_) ;
-
-
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";
-
$sth = $dbh->prepare($s);
-
$sth->execute();
-
-
}
-
-
=cut
-
sub getNextField {
-
my $s = shift;
-
my $delimiter = shift;
-
$delimiter or $delimiter = "\",\"";
-
my $endPos = index $s, "$delimiter";
-
$delimiter eq "," and $endPos++;
-
$delimiter eq "\n" and $endPos++;
-
$delimiter eq "\",\"" and $endPos+=2;
-
my $field = substr $s, 0, $endPos, "";
-
$field = substr $field, 0, -1;
-
(substr $field, -1) eq "\"" and $field = substr $field, 0 - 1;
-
(substr $field, 0, 1) eq "\"" and $field = substr $field, 1;
-
$field = $dbh->quote($field);
-
return $field, $s;
-
}
-
-
=cut
-
}
-
}
-
-
else {
-
die("Problem connecting to : $DSN\n");
-
}
-
-
print "\n\nEnd.\n";
-
exit;
-
-
-
sub usage() {
-
print_header();
-
print "\nUsage: <cdr_log_file> <sql_hostname> <Database> <Table> <Username> <Password>";
-
print "\n\nTo see the Expected database format Database_format\n\nEnd.\n";
-
die;
-
};
-
-
sub dbformat()
-
{
-
print_header();
-
print "\n Expects a table containing the following fields:\n
-
accountcode varchar(80) DEFAULT ''::character varying NOT NULL,
-
src varchar(80) DEFAULT ''::character varying NOT NULL,
-
dst varchar(80) DEFAULT ''::character varying NOT NULL,
-
dcontext varchar(80) DEFAULT ''::character varying NOT NULL,
-
clid varchar(80) DEFAULT ''::character varying NOT NULL,
-
channel varchar(80) DEFAULT ''::character varying NOT NULL,
-
dstchannel varchar(80) DEFAULT ''::character varying NOT NULL,
-
lastapp varchar(80) DEFAULT ''::character varying NOT NULL,
-
lastdata varchar(80) DEFAULT ''::character varying NOT NULL,
-
start datetime with time zone DEFAULT now() NOT NULL,
-
answer datetime with time zone DEFAULT now() NOT NULL,
-
[end] datetime with time zone DEFAULT now() NOT NULL,
-
duration int DEFAULT (0)::bigint NOT NULL,
-
billsec int DEFAULT (0)::bigint NOT NULL,
-
disposition varchar(80) DEFAULT ''::character varying NOT NULL,
-
amaflags varchar(80) DEFAULT ''::character varying NOT NULL,
-
accountcode character varying(20) DEFAULT ''::character varying NOT NULL,
-
unquie character varying(255) DEFAULT ''::character varying NOT NUL,
-
userfield character varying(255) DEFAULT ''::character varying NOT NULL
-
";
-
die;
-
};
-
-
sub print_header()
-
{
-
print "\nimport.pl - Load Asterisk CDR datas to SQL Server database\n";
-
print "\n==============Storing data into SQL Server================\n";
-
};
Please help me....
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. -
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
use Text::CSV_XS;
-
use Data::Dumper;
-
-
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","");
-
-
my $csv = Text::CSV_XS->new ( { binary => 1 } )
-
or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
-
-
$csv->parse($str);
-
my @fields = $csv->fields;
-
-
print Dumper \@fields;
Outputs: -
$VAR1 = [
-
'',
-
'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',
-
''
-
];
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. -
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
use Text::CSV_XS;
-
use Data::Dumper;
-
-
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","");
-
-
my $csv = Text::CSV_XS->new ( { binary => 1 } )
-
or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
-
-
$csv->parse($str);
-
my @fields = $csv->fields;
-
-
print Dumper \@fields;
Outputs: -
$VAR1 = [
-
'',
-
'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',
-
''
-
];
thanks a lot RonB sir ...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,......
|
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
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |