Subroutine for insert template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanponnapalli
    New Member
    • May 2008
    • 51

    Subroutine for insert template

    hi,
    I have designed a module, which contains a template for insert command.
    The code is as under:
    Code:
          my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                   localtime(time);
          $year+=1900;
          $mon++;                           	                 
         
         my $col_name = "100"; 
         my $ins_name = "pavan";
         my $ins_id = 3;
         my $ins_desc = "hi";
         my $createdby = 1;
         my $modifiedby = 1;
         my $table_name = Institution;
         my (@colnames,@values,@exec_arr);
         my $now='now';
        push(@colnames,Institution_Id,Institution_Name,Institution_Desc,Createdby,Createddtm,Modifiedby,Modifieddtm,Customer_Id);
         my $count = @colnames; 
         push(@values,$ins_id,$ins_name,$ins_desc,$createdby,$now,$modifiedby,$now,100);  
         #print "@colnames";
          #my @arr2 = sub1($table_name,@colnames,@values);
         sub1($table_name,@colnames,@values);
         
         sub sub1
         {
         	
             push(my @arr,@_);
             print "@arr";
             my $tab_name = $arr[0];
             shift(@arr);
             print "--------------------------------------- \n";
             print "$arr[0]";
             print "--------------------------------------- \n";
             print "\n<<@arr>>";
             print " \n$tab_name \n";
             my $count = scalar(@arr);
            
         
             print "<<$count>>> \n";
             my $limit = $count/2;
             
    
             $i=0;
             my $var='';
     	     print "<$limit> \n";		
             while($i<$limit)
             {
             	if( ($i) == $limit-1 )
             	{
             		$var = $var . $arr[$i];
             	}
             	else
             	{
             		$var = $var . $arr[$i] . ",";
             	}
             	$i++;	
             }
             print "\n var:::::::$var";
             
             $i=$limit++;
             my $vals='';
     	
             while($i<$count)
             {
             	if( ($i) == $count-1 )
             	{
             		$vals = $vals . $arr[$i];
             	}
             	else
             	{
             		$vals = $vals . $arr[$i] . ",";
             	}	
             	$i++;
             }
             print "\n vals:::::::$vals \n";
             my $sth = $dbh->prepare("insert into $tab_name ($var) values ($vals)");
             my $resp = $sth->execute();
             if($resp ne '0E0')
             {
             	print "Successfully inserted";
             }
                  
         }
    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
    Last edited by numberwhun; Sep 12 '08, 11:58 AM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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:

    Code:
    @array = ($var, $var2, $var3, etc, etc, etc);
    incorrect:

    Code:
    push @array,$var, $var2, $var3, etc, etc, etc;

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      your strings should also all be quoted, for example:

      Code:
      push(@colnames,Institution_Id,Institution_Name,Ins titution_Desc,Createdby,Createddtm,Modifiedby,Modifieddtm,Customer_Id);
      should be:

      Code:
      @colnames = qw(Institution_Id Institution_Name Institution_Desc Createdby Createddtm Modifiedby Modifieddtm Customer_Id);

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Also, please note that your closing code tag should have a "/' character, not a "%" character.

        Regards,

        Jeff

        Comment

        Working...