Perl sql table upload variable problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl sql table upload variable problem
# 1  
Old 12-01-2006
Perl sql table upload variable problem

Hi,

I wrote a perl script to create and upload data to a mysql data base. Please see the script as shown below,
Code:
#!/usr/bin/perl -w

#Defenetion of modules use in this scrpt
use CGI qw(:standard);
use DBI ;


my $datetable1 = `date +%Y%m%d`;
my $datetable = $datetable1 ;
print ${datetable} ;                                                                                                         
# set the data source name
# format: dbi:db type:db name:host:port
# mysqls default port is 3306
# if you are running mysql on another host or port,
#you must change it                                                                                                                                                                                                                                       
my $dsn = 'dbi:mysql:ppscdr:localhost:3306';

# set the user and password

my $user = 'perl';
my $pass = 'perl';                                                                                                           
# now connect and get a database handle

  my $dbh = DBI->connect($dsn, $user, $pass)
            or die "Cant connect to the DB: $DBI::errstr\n";

my $sth_rec = $dbh->prepare("CREATE TABLE rec_$datetable (CallType  CHAR(1) NOT NULL, CallingPartyNumber
                             CHAR(32) NOT NULL, CalledPartyNumber CHAR(32) NOT NULL, CallBeginTime CHAR(14) NOT NULL,
                             CallDuration int(8) NOT NULL, NormalFee int(8) NOT NULL,
                             NormaAccountBalanceAfterCall int(8) NOT NULL)");

    $sth_rec->execute;

    my $sth1_rec_u = $dbh->prepare("LOAD DATA INFILE '/ppscdr/cdrrec/combined/$datetable.cdrrec.txt'
                              INTO TABLE rec_$datetable FIELDS TERMINATED BY '|'");

    $sth1_rec_u->execute;


my $sth_smr = $dbh->prepare("CREATE TABLE smr_$datetable (CallingPartyNumber CHAR(32) NOT NULL,
                         CalledPartyNumber CHAR(32) NOT NULL, FeeDiductionType int(1) NOT NULL,
                         NormalFee int(8) NOT NULL, SmsDateTime CHAR(14) NOT NULL,
                         NormaAccountBalanceAfterSMS int(8) NOT NULL)");

    $sth_smr->execute;

    my $sth_smr_u = $dbh->prepare("LOAD DATA INFILE '/ppscdr/cdrsmr/combined/$datetable.cdrsmr.txt'
                              INTO TABLE smr_$datetable FIELDS TERMINATED BY '|'");

    $sth_smr_u->execute;

my $sth_p2p = $dbh->prepare("CREATE TABLE p2p_$datetable (CallingPartyNumber CHAR(32) NOT NULL,
                         CalledPartyNumber CHAR(32) NOT NULL,
                         TransferDateTime CHAR(14) NOT NULL, TransferBalance int(16) NOT NULL,
                         SubCosIDsnd int(8) NOT NULL, SubCosIDrsv int(8) NOT NULL,
                         TransferFee int(16) NOT NULL)");

    $sth_p2p->execute;

    my $sth_p2p_u = $dbh->prepare("LOAD DATA INFILE '/ppscdr/cdrp2p/combined/${datetable}.cdrp2p.txt'
                              INTO TABLE p2p_$datetable FIELDS TERMINATED BY '|'");

    $sth_p2p_u->execute;

It giving following error, please sombody tell me how can i resolve this error,

Code:
./auto_sql_upload.pl
20061201
DBD::mysql::st execute failed: Can't get stat of '/ppscdr/cdrrec/combined/20061201
.cdrrec.txt' (Errcode: 2) at ./auto_sql_upload.pl line 37.
DBD::mysql::st execute failed: Can't get stat of '/ppscdr/cdrsmr/combined/20061201
.cdrsmr.txt' (Errcode: 2) at ./auto_sql_upload.pl line 50.
DBD::mysql::st execute failed: Can't get stat of '/ppscdr/cdrp2p/combined/20061201
.cdrp2p.txt' (Errcode: 2) at ./auto_sql_upload.pl line 63. :mad:

# 2  
Old 12-01-2006
You need to chomp your date variable. It's looking for a file with a newline character in it. Add the line below in red.

Code:
my $datetable1 = `date +%Y%m%d`;
chomp $datetable1;
my $datetable = $datetable1 ;
print ${datetable} ;

Whenever you execute a shell command from Perl, it will most likely have the newline character attached to the output.
# 3  
Old 12-01-2006
Thanks for the support. Scrip is working fine after modification done according to your advice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. UNIX and Linux Applications

SQL - how to alter info of a cell in table

Hello, I am newbie on mysql and trying to edit my database from terminal under linux. What I need to do is to change the information written in a cell in table. Let me explain what I tried: $ mysql -u mysqluser -p $ show databases; $ USE catalogue; $ show tables ; $ select * from... (2 Replies)
Discussion started by: baris35
2 Replies

3. Solaris

SQL QUERY to Table Output

Hi I am trying to run sql query from solaris in csh script and send the output to email. Below is my sql query select p.spid,se.program seprogram, se.machine, se.username, sq.sql_text,sq.retrows from v$process p inner join v$session se on p.addr = se.paddr inner join ( select... (2 Replies)
Discussion started by: tharmendran
2 Replies

4. Programming

Sql developer how to upload the excel sheet in Oracle table

I have some records to be updated in oracle table. I am using sql developer tool. could any one tell me how to update those records in oracle table. I am having excel sheet with those records. (4 Replies)
Discussion started by: ramkumar15
4 Replies

5. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

6. Shell Programming and Scripting

sftp file upload problem

Hi All, I am trying to upload a text file from HP unix to Windows tectia server using sftp, the text file shows with new line character after upload. For EG : abc.txt file contains 123 456 aftre upload it shows as 123 456 i am using sftp version 2.0 TQ, (4 Replies)
Discussion started by: phani1312
4 Replies

7. Shell Programming and Scripting

Problem while storing sql query value in a variable

Hi, When i execute the below statement , the value is not getting stored in the variable. AnneeExercice=`sqlplus $LOGSQL/$PASSWORDSQL << FIN >> $GEMOLOG/gemo_reprev_reel_data_ventil_$filiale.trc SELECT bi09exercice FROM bi09_scenario WHERE bi09idfiliale=UPPER('de') AND ... (1 Reply)
Discussion started by: krishna_gnv
1 Replies

8. HP-UX

upload oracle table

am on HP-Unix and want to upload table on windows oracle from HP-Unix Thanx Swapnil (2 Replies)
Discussion started by: swapnil286
2 Replies

9. UNIX for Advanced & Expert Users

Upload of the images from the folder to the Database table

Hi all, i am new to the unix enviorment i have got a urgent requirement where we need to migrate the date from the folder heirachy that contains the "IMAGES". These images are to be uploaded on to the database table. Uploading images from the a single folder (Static) to the... (0 Replies)
Discussion started by: shashisaini24
0 Replies

10. Shell Programming and Scripting

stuck in perl cgi to upload a file to server

hi, i m working on a perl cgi script which uploads a file to the server. i m stuck. i hav written the errors. plz help. Sachin Kaw ______________________________________________________________________ #!/usr/bin/perl -w use CGI; use CGI qw(:standard); use strict; use POSIX... (4 Replies)
Discussion started by: sachin_kaw
4 Replies
Login or Register to Ask a Question