By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,176 Members | 984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,176 IT Pros & Developers. It's quick & easy.

Subroutine for insert template

P: 51
hi,
I have designed a module, which contains a template for insert command.
The code is as under:
Expand|Select|Wrap|Line Numbers
  1.       my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  2.                                                localtime(time);
  3.       $year+=1900;
  4.       $mon++;                                                
  5.  
  6.      my $col_name = "100"; 
  7.      my $ins_name = "pavan";
  8.      my $ins_id = 3;
  9.      my $ins_desc = "hi";
  10.      my $createdby = 1;
  11.      my $modifiedby = 1;
  12.      my $table_name = Institution;
  13.      my (@colnames,@values,@exec_arr);
  14.      my $now='now';
  15.     push(@colnames,Institution_Id,Institution_Name,Institution_Desc,Createdby,Createddtm,Modifiedby,Modifieddtm,Customer_Id);
  16.      my $count = @colnames; 
  17.      push(@values,$ins_id,$ins_name,$ins_desc,$createdby,$now,$modifiedby,$now,100);  
  18.      #print "@colnames";
  19.       #my @arr2 = sub1($table_name,@colnames,@values);
  20.      sub1($table_name,@colnames,@values);
  21.  
  22.      sub sub1
  23.      {
  24.  
  25.          push(my @arr,@_);
  26.          print "@arr";
  27.          my $tab_name = $arr[0];
  28.          shift(@arr);
  29.          print "--------------------------------------- \n";
  30.          print "$arr[0]";
  31.          print "--------------------------------------- \n";
  32.          print "\n<<@arr>>";
  33.          print " \n$tab_name \n";
  34.          my $count = scalar(@arr);
  35.  
  36.  
  37.          print "<<$count>>> \n";
  38.          my $limit = $count/2;
  39.  
  40.  
  41.          $i=0;
  42.          my $var='';
  43.           print "<$limit> \n";        
  44.          while($i<$limit)
  45.          {
  46.              if( ($i) == $limit-1 )
  47.              {
  48.                  $var = $var . $arr[$i];
  49.              }
  50.              else
  51.              {
  52.                  $var = $var . $arr[$i] . ",";
  53.              }
  54.              $i++;    
  55.          }
  56.          print "\n var:::::::$var";
  57.  
  58.          $i=$limit++;
  59.          my $vals='';
  60.  
  61.          while($i<$count)
  62.          {
  63.              if( ($i) == $count-1 )
  64.              {
  65.                  $vals = $vals . $arr[$i];
  66.              }
  67.              else
  68.              {
  69.                  $vals = $vals . $arr[$i] . ",";
  70.              }    
  71.              $i++;
  72.          }
  73.          print "\n vals:::::::$vals \n";
  74.          my $sth = $dbh->prepare("insert into $tab_name ($var) values ($vals)");
  75.          my $resp = $sth->execute();
  76.          if($resp ne '0E0')
  77.          {
  78.              print "Successfully inserted";
  79.          }
  80.  
  81.      }  
  82.  
I am able to get column names by $var scalar , and all the values in $vals,
but i am getting error while executing the query as under:

DBD::mysql::st execute failed: Unknown column 'pavan' in 'field list' at D:/pavan/Workspace/sample/insert_template.pl line 107.
Use of uninitialized value in string ne at D:/pavan/Workspace/sample/insert_template.pl line 108.

how to correct the mistake?

Thanks & Regards,
Pavan
Sep 12 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
I don't know what the problem is, but looking at your code, you are using the push() function for no reason. You assign initial values to an array using the assignment operator "=", not push().

correct:

Expand|Select|Wrap|Line Numbers
  1. @array = ($var, $var2, $var3, etc, etc, etc);
  2.  
incorrect:

Expand|Select|Wrap|Line Numbers
  1. push @array,$var, $var2, $var3, etc, etc, etc;
Sep 12 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
your strings should also all be quoted, for example:

Expand|Select|Wrap|Line Numbers
  1. push(@colnames,Institution_Id,Institution_Name,Ins titution_Desc,Createdby,Createddtm,Modifiedby,Modifieddtm,Customer_Id);
should be:

Expand|Select|Wrap|Line Numbers
  1. @colnames = qw(Institution_Id Institution_Name Institution_Desc Createdby Createddtm Modifiedby Modifieddtm Customer_Id);
Sep 12 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Also, please note that your closing code tag should have a "/' character, not a "%" character.

Regards,

Jeff
Sep 12 '08 #4

Post your reply

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