pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Garner <peter_garner@yahoo.com>
To: Atika <agoswa@essex.ac.uk>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] string containing (')
Date: Sun, 21 Mar 1999 16:58:55 -0800 (PST)
Message-ID: <19990322005855.7207.rocketmail@send205.yahoomail.com> (raw)

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




view thread (5+ messages)  latest in thread

Message-ID: <19990322005855.7207.rocketmail@send205.yahoomail.com>
Permalink:  ../19990322005855.7207.rocketmail@send205.yahoomail.com/
Also on:    postgresql.org/message-id/19990322005855.7207.rocketmail@send205.yahoomail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: peter_garner@yahoo.com, agoswa@essex.ac.uk
  Subject: Re: [SQL] string containing (')
  In-Reply-To: <19990322005855.7207.rocketmail@send205.yahoomail.com>
  References: <19990322005855.7207.rocketmail@send205.yahoomail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox