Thread: Re: [SQL] string containing (')

Re: [SQL] string containing (')

From
Peter Garner
Date:
Hi Atika!

You need to escape the apostrophe, in other words,

"Let's all get stoned"

must be converted to

"Let\'s all get stoned"

Here is some java that does this :

  /**
   *  Massages a <code>String</code> parameter so that parameter is
palatable to the Database.  For
   *  example, "It's a good day!" would be converted to "It\'s a good
day!".
   *
   *  @param      string    The <code>String</code> we wish to massage
   *
   *  @return     A "fixed" version of the Argument
<code>String</code> Object
   */
  public static String fixupString ( String string )
  {
    //  Assume a worst case scenario where EVERY byte in the string
needs to be escaped, i.e. make the Length of the NEW
    //  Buffer twice the Length of the old Buffer
    char  cCurrent      = '\0'  ;
    char  carrBuffer [] = new char  [string.length () * 2]  ;
    int   nBuffIdx      = 0 ;

    for ( int n = 0 ; n < string.length () ; n++ )
    {
      cCurrent  = string.charAt ( n ) ;

      //  See if the current character is an apostrophe or an '\'.  I
think the only reliable way to escape all apostrophes
      //  and all '\' is to escape each and every one that is
encountered.  Otherwise one may not get the original Text back
      //  out of the DBMS.  For example if we have the string
"1234\'5678" and we do not alter it since the apostrophe is
      //  already escaped, we will get the string "1234'5678" when we
retrieve it FROM the DBMS!
      if ( charNeedsEscaping ( cCurrent ) )
      {
        carrBuffer [nBuffIdx++] = '\\'  ;

      } /* endif */

      carrBuffer [nBuffIdx++] = cCurrent  ;

    } /* endfor */

    String  strgReturn  = new String ( carrBuffer , 0 , nBuffIdx ) ;

    return  strgReturn  ;

  } /* static String fixupString ( String string ) */






---Atika <agoswa@essex.ac.uk> wrote:
>
> HI!
> Can anyone help,
> I want to store a string into a relation that might contain an
apostrophe
> (').  Is there anyway around this or can I not store such strings.
> Thanks for your help

==
Peace,
Peter

We are Microsoft of Borg, you will be assimilated!!!
Resistance is fut...  ***BZZZRT***  THUD!!!
[General Protection Fault in MSBorg32.DLL]
Please contact the vendor of this Borg for more information
_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com