how to insert a single quote inside a perl scalar

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

    how to insert a single quote inside a perl scalar

    hi,
    I need to send some values to a subroutine where i use insert query.
    suppose say my scalar is as follows:

    my ($name,$number, $address) etc and i get the values into those scalars dynamically by some other process. When i send these values to the subroutine containing an insert query, there for the columns containing
    varchar i need to have ' some value' for int no quotes are requried. For
    example if we take $name, the Name column in the database is varchar.
    So value inside $name need to have ' ' (A single quote). For number it is not the case . If i do assign the values as $name="'Pavan'" (here i am including single quotes inside double quotes). But when the data comes from some other source , it comes as $name="pavan" and when i pass this value to a subroutine,
    to the query , the query becomes something like
    insert into employee (Empno,Ename,Ad dress) values (1,pavan,add);
    It need to be (1,'pavan','add '); So, please tell me how to insert single quotes.

    Thanks & Regards,
    Pavan
  • aurekha
    New Member
    • Sep 2007
    • 34

    #2
    if your scalar i.e $name = 'pavan';

    Inorder to insert into database, escape the singlequotes.

    i.e $name = "\'pavan\'"

    then you end with singlequotes as 'pavan'

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      Try this on your SQL statement. This should eliminate the need for the quotes.

      Code:
      my $insert = $dbh->prepare(qq{INSERT INTO table_names($col1, $col2) VALUES(?,?)});
         $insert->execute($value1, $value2);
         $insert->finish();
      --Kevin

      Comment

      • pavanponnapalli
        New Member
        • May 2008
        • 51

        #4
        Dear friends,
        I am sorry to tell you that you did not catch me properly.
        Actually i have $name=pavan. So since it is set as varchar in the database, the insert module i have written need to have single quotes as
        insert into $tablename ($colnames) values ($values);
        and $colnames contains string as (Name,Desig,... .)
        and $values contains ($name,$desig). .. etc
        Now, if Name column in the database is set as int, there would not have been any problem. But since $name is set as varchar, we need to have the value as
        'pavan' . But since i have got pavan,it is copied as pavan in $name and hence the error. I have worked on that and got this solution.
        my $name1 = "'$name'" (single quotes within double quotes).
        Any other solution is highly appreciated.

        Thanks & Regards,
        Pavan kumar

        Comment

        • pavanponnapalli
          New Member
          • May 2008
          • 51

          #5
          wish,
          I got u. I am sorry to send u a post, without understanding ur theorey.
          I will definitly try that.

          Thanks ,
          Pavan

          Comment

          Working...