For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > replacing characters in a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author replacing characters in a string
Perldiscuss - Perl Newsgroups And Mailing Lists

2004-05-22, 11:32 am

I cant find a simple perl function to replace characters in a string. Im
trying to inserts strings like "bob's" into a database and need to convert
that to "bob''s" so that sql doesnt whine when i do an insert.

Any suggestions?

Sapet Frederic

2004-05-22, 11:32 am

On 18 May 2004 19:12:19 -0000
jason@greaterthanone.com (PerlDiscuss - Perl Newsgroups and mailing lists) =
wrote:

> I cant find a simple perl function to replace characters in a string. Im
> trying to inserts strings like "bob's" into a database and need to convert
> that to "bob''s" so that sql doesnt whine when i do an insert.
>=20
> Any suggestions?


hi

try to use DBI module
see perldoc DBI and quote method


quote

$sql =3D $dbh->quote($value);
$sql =3D $dbh->quote($value, $data_type);

Quote a string literal for use as a literal value in an SQL statement, =
by escaping any special characters (such as quotation marks) contained with=
in the string and adding the required type of outer quotation marks.

$sql =3D sprintf "SELECT foo FROM bar WHERE baz =3D %s",
$dbh->quote("Don't");

For most database types, quote would return 'Don''t' (including the out=
er quotation marks).



>=20
>=20
> --=20
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20



--=20
Fr=E9d=E9ric Sapet

***********************
G=E9noplante-Info
523 place des Terrasses
91000 Evry
01 60 87 37 59
fsapet@infobiogen.fr
***********************
Wiggins D Anconia

2004-05-22, 11:32 am

> I cant find a simple perl function to replace characters in a string. Im
> trying to inserts strings like "bob's" into a database and need to convert
> that to "bob''s" so that sql doesnt whine when i do an insert.
>
> Any suggestions?
>


There are regex's and the tr/y functions/operators? But in this case you
shouldn't use either and instead use binding (assuming you are using
DBI, are you?). Binding allows DBI/DBD which is very smart decide how
the data needs to be quoted rather than attempting to do this manually
which usually just leads to problems.

http://search.cpan.org/~timb/DBI-1....and_Bind_Values

Alternatively if binding can't be used, there is the 'quote' method for
handling this specific task (on same page as above).

http://danconia.org

Perldiscuss - Perl Newsgroups And Mailing Lists

2004-05-22, 11:32 am

- Yes I am using DBI. That's perfect, I didnt realize they had this
functionality, but it makes alot of sense that they do. Thank you, thank
you.

Wiggins D Anconia wrote:

[color=darkred]
> There are regex's and the tr/y functions/operators? But in this case you
> shouldn't use either and instead use binding (assuming you are using
> DBI, are you?). Binding allows DBI/DBD which is very smart decide how
> the data needs to be quoted rather than attempting to do this manually
> which usually just leads to problems.


> http://search.cpan.org/~timb/DBI-1....and_Bind_Values


> Alternatively if binding can't be used, there is the 'quote' method for
> handling this specific task (on same page as above).


> http://danconia.org



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com