Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Escaped characters

Reply
Thread Tools

Escaped characters

 
 
Ruby Tuesday
Guest
Posts: n/a
 
      04-12-2004
Hi, I was wondering if there are such a function in Ruby for escaping a
character, e.g:

I'd like to add record to mysql db but it has quote('), double-quote("), and
other escape-able characters.

How do I escape those characters just before pumping it to the database?

Now the strings is clobbered with escape characters, how do I strip it for
searching? Thanks


 
Reply With Quote
 
 
 
 
Jim Weirich
Guest
Posts: n/a
 
      04-12-2004

Ruby Tuesday said:
> Hi, I was wondering if there are such a function in Ruby for escaping a
> character, e.g:
>
> I'd like to add record to mysql db but it has quote('), double-quote("),
> and
> other escape-able characters.
>
> How do I escape those characters just before pumping it to the database?
>
> Now the strings is clobbered with escape characters, how do I strip it for
> searching? Thanks


Are you using DBI? If so, then form your SQL queries with "?"
placeholders and pass the actual values when the queries are executed.
DBI will handle all the proper quoting and escaping for you.

For example.

db = DBI.connect("DBI:yada:yada", user, pw)
db.do("UPDATE this_table SET a_column = ? WHERE yada = yada",
%{This is is automatically 'quoted' and "escaped" by DBI})

There may be similiar functionality built into the direct DB bindings. If
so, this is generally a better choice than self escaping.

However, DBI does provide an escape unility function you can call
yourself. Given a database handle (such as "db" above) you can use
db.quote(string).

--
-- Jim Weirich http://www.velocityreviews.com/forums/(E-Mail Removed) http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-13-2004

"Jim Weirich" <(E-Mail Removed)> schrieb im Newsbeitrag
news:28107.192.223.163.6.1081789190.squirrel@weiri chhouse.org...
>
> Ruby Tuesday said:
> > Hi, I was wondering if there are such a function in Ruby for escaping

a
> > character, e.g:
> >
> > I'd like to add record to mysql db but it has quote('),

double-quote("),
> > and
> > other escape-able characters.
> >
> > How do I escape those characters just before pumping it to the

database?
> >
> > Now the strings is clobbered with escape characters, how do I strip it

for
> > searching? Thanks

>
> Are you using DBI? If so, then form your SQL queries with "?"
> placeholders and pass the actual values when the queries are executed.
> DBI will handle all the proper quoting and escaping for you.
>
> For example.
>
> db = DBI.connect("DBI:yada:yada", user, pw)
> db.do("UPDATE this_table SET a_column = ? WHERE yada = yada",
> %{This is is automatically 'quoted' and "escaped" by DBI})
>
> There may be similiar functionality built into the direct DB bindings.

If
> so, this is generally a better choice than self escaping.


If you don't want to use that there's always String#gsub to accomplish
such substitutions - in either direction, e.g.

str.gsub(/(['"])/, '^\\1' )
str.gsub(/^(.)/, '\\1' )

with "^" beeing the escape char in this example.

robert


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
servlet request handling - escaped characters in request Thea Java 2 09-22-2006 12:08 PM
Convert Raw Text Escaped Characters to Characters nicholas.wakefield@gmail.com Java 2 07-11-2005 09:17 PM
Minidom output of XML escaped characters Derek Basch Python 2 03-25-2005 05:25 PM
Translating escaped characters Henrik S. Hansen Python 2 11-02-2004 07:12 AM
Translate escaped characters in a string Ben Finney Python 2 04-03-2004 07:46 AM



Advertisments