Click to See Complete Forum and Search --> : Database, Perl and escaping quotes


erum
01-25-2004, 11:50 AM
Hi,

I want to save certain data in a mysql table field. I'm a newbie at this and just want to modify a script a bit.

How do I escape ' and " while saving and how do I retrieve the data so that the data comes up fine?

The script is in Perl.

Thx!

crh3675
01-25-2004, 11:54 AM
Use the escape character "\" preceding a quote or single quote:

example

$str="what\'s your name?";

--Craig

erum
01-25-2004, 12:31 PM
What do I need to do so that it gets automatically entered before the ' and ". See I'm not entering the data. Someone else is and they are gonna be entering full paras and not going to add \ before every quote. So I need to know how to do that automatically.

Plus when the data is displayed back I need to be able to remove the \.

What code do I need to add for these two things?

garfvader
01-25-2004, 12:45 PM
Then you need to use the perl replace ability.
Take their data and put it in something such as an array. Then run a for statement on the array and something like this:

for $line (@yourdata)
{
$line =~ s/'/\'/g;
$line =~ s/"/\"/g;
}

What that line does is it takes every instance of say " it finds and replaces it with \" and it does the same thing with '. I know there's a more refined way to do that in less lines of code as well with a little research.

Nedals
02-05-2004, 09:32 PM
my $dbh = DBI->connect('DBI:mysql:database_name','user','passwd');
...

in your sql statement

my $sql = "...".$dbh->quote($yourdata)."....";
This will automatically quote the data as needed