468,316 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

how to insert a single quote inside a perl scalar

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,Address) values (1,pavan,add);
It need to be (1,'pavan','add'); So, please tell me how to insert single quotes.

Thanks & Regards,
Sep 12 '08 #1
4 3594
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'
Sep 12 '08 #2
971 Expert 512MB
Try this on your SQL statement. This should eliminate the need for the quotes.

Expand|Select|Wrap|Line Numbers
  1. my $insert = $dbh->prepare(qq{INSERT INTO table_names($col1, $col2) VALUES(?,?)});
  2.    $insert->execute($value1, $value2);
  3.    $insert->finish();
Sep 12 '08 #3
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
Sep 12 '08 #4
I got u. I am sorry to send u a post, without understanding ur theorey.
I will definitly try that.

Thanks ,
Sep 12 '08 #5

Post your reply

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

Similar topics

5 posts views Thread by John Smith | last post: by
5 posts views Thread by Robert Oschler | last post: by
6 posts views Thread by Frederik Vanderhaeghe | last post: by
6 posts views Thread by Allan Stirrett | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.