Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 62EA1D41746 for ; Sat, 5 Nov 2011 08:55:56 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 54785-03 for ; Sat, 5 Nov 2011 11:55:49 +0000 (UTC) Received: from mx2.hub.org (mx2.hub.org [200.46.204.254]) by mail.postgresql.org (Postfix) with ESMTP id DF6FED41744 for ; Sat, 5 Nov 2011 08:55:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from newserver.jfcomputer.com (unknown [76.14.161.106]) by mx2.hub.org (Postfix) with ESMTP id 30C7717508CF for ; Fri, 4 Nov 2011 21:12:33 -0300 (ADT) Received: from localhost (unknown [127.0.0.1]) by newserver.jfcomputer.com (Postfix) with ESMTP id C69245E09 for ; Sat, 5 Nov 2011 00:12:10 +0000 (UTC) X-Virus-Scanned: amavisd-new at site Received: from newserver.jfcomputer.com ([127.0.0.1]) by localhost (linux-jfp8.site [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id RHKuCNjdXwS6 for ; Fri, 4 Nov 2011 17:12:10 -0700 (PDT) Received: from linux-desktop.localnet (unknown [192.168.1.143]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johnf@jfcomputer.com) by newserver.jfcomputer.com (Postfix) with ESMTPSA id 043962C9C for ; Fri, 4 Nov 2011 17:12:09 -0700 (PDT) From: John Fabiani Organization: J.F. Computer Solutions To: pgsql-sql@postgresql.org Subject: Re: the use of $$string$$ Date: Fri, 4 Nov 2011 17:12:09 -0700 User-Agent: KMail/1.13.5 (Linux/2.6.34.10-0.2-desktop; KDE/4.4.4; i686; ; ) References: <201111040738.29303.johnf@jfcomputer.com> <201111040826.16177.johnf@jfcomputer.com> <4EB429AD.3030607@archonet.com> In-Reply-To: <4EB429AD.3030607@archonet.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201111041712.09607.johnf@jfcomputer.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201111/21 X-Sequence-Number: 35994 On Friday, November 04, 2011 11:06:37 am Richard Huxton wrote: > On 04/11/11 15:26, John Fabiani wrote: > > On Friday, November 04, 2011 07:38:29 am John Fabiani wrote: > >> Hi, > >> I just discovered that I can use $$string$$ to account for the problem > >> of single quotes in the string (or other strange char's). However, I > >> noticed that the table field contained E'string'. I actually tried to > >> find info on this but I did not find anything. > >> > >> Could someone explain what it means or better provide a web link for me > >> to discover the info. > > > > Thank you both for the quick replies and the links. > > > > What I still do not understand (I'm a little slow) is the fact that > > pgadmin3 showed the data as E'string'. So I'm wondering if the data is > > now different in the sense that I need use the 'E' in the field. > > I think you're missing some bit of the puzzle here. > > There's the universal problem of how to deal with quote marks inside > quotes. The SQL standard says you double the quote. > > SELECT length('ab''de'); > length > -------- > 5 > > Traditionally, PostgreSQL let you use backslash escapes too, not just > for single quotes but for some other common C-style idioms. > > SELECT length(E'ab\'de'); > length > -------- > 5 > > For the last few years, this has been moved into its own quoting style > so standard strings are always well, standard. > > SELECT length('ab\nde'); > length > -------- > 6 > > SELECT length(E'ab\nde'); > length > -------- > 5 > > The [E'] is an opening quote - both characters. It isn't part of the > value at all. If a field contains "E'" then you put it there, possibly > by quoting something in pgadmin that was already quoted. > > Likewise you can use $$..$$ to quote strings (actually $$whatever$$). > That gives you sql-standard escaping apart from single quotes. It's > especially useful for function bodies which tend to contain their own > string literals. > > SELECT length($$ab\nde$$); > length > -------- > 6 > > None of this should matter from an application however, since its > database access library should do all the quoting for you. > > HTH thanks for the reply. I'm using psycopg2. This is what I'm doing from python myvarString = "long string that contains single quotes" cusor.execute("insert into table (pkid, myfield) values (%s, $$%s$$)",(123, myvarString)) When I execute the above I'm seeing: E'long string that contains single quotes' in the field. When I do a "select * from table" I get E'long string that contains single quotes'. If I do myvarString = "long string that without single quotes" cusor.execute("insert into table (pkid, myfield) values (%s, %s)",(123, myvarString)) I get the following: "long string that without single quotes" I have to be doing something wrong. But where? Johnf