Escaped characters

Discussion in 'Ruby' started by Ruby Tuesday, Apr 12, 2004.

  1. Ruby Tuesday

    Ruby Tuesday Guest

    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
     
    Ruby Tuesday, Apr 12, 2004
    #1
    1. Advertising

  2. Ruby Tuesday

    Jim Weirich Guest

    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://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)
     
    Jim Weirich, Apr 12, 2004
    #2
    1. Advertising

  3. "Jim Weirich" <> schrieb im Newsbeitrag
    news:...
    >
    > 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
     
    Robert Klemme, Apr 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    765
  2. Ben Finney
    Replies:
    2
    Views:
    341
    Josiah Carlson
    Apr 3, 2004
  3. Henrik S. Hansen

    Translating escaped characters

    Henrik S. Hansen, Nov 1, 2004, in forum: Python
    Replies:
    2
    Views:
    290
    Henrik S. Hansen
    Nov 2, 2004
  4. Derek Basch
    Replies:
    2
    Views:
    462
    Derek Basch
    Mar 25, 2005
  5. Thea
    Replies:
    2
    Views:
    628
Loading...

Share This Page