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

how to insert a single quote inside a perl scalar

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

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


P: 34
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

eWish
Expert 100+
P: 971
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();
--Kevin
Sep 12 '08 #3

P: 51
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

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

Thanks ,
Pavan
Sep 12 '08 #5

Post your reply

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