Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 2BF589FB29D for ; Sat, 27 Jan 2007 21:25:48 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 88474-03 for ; Sat, 27 Jan 2007 21:25:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id E68799FB27A for ; Sat, 27 Jan 2007 21:25:42 -0400 (AST) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 76CEE30E44; Sun, 28 Jan 2007 02:25:40 +0100 (MET) From: Tobias Thierer X-Newsgroups: pgsql.interfaces.jdbc Subject: Re: escape string for pgsql (using jdbc/java)? Date: Sun, 28 Jan 2007 14:25:40 +1300 Organization: Hub.Org Networking Services Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.9) Gecko/20061211 SeaMonkey/1.0.7 In-Reply-To: To: pgsql-jdbc@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50 X-Spam-Level: X-Archive-Number: 200701/174 X-Sequence-Number: 16539 Kris Jurka wrote: >> 1.) Is there a built-in method somewhere in the jdbc driver that escapes >> strings and makes them safe to use in an SQL statement (inside a >> string)? > > There is org.postgresql.core.Utils#appendEscapedString, but it's not > something we support or advertise. It's really for internal use only. I dislike that this method expects me to tell it whether i have standard_conforming_strings set - this kinda defeats the "write once, run everywhere" principle. If I replace \ with \\ and DO have standard_conforming_strings set, then this will actually create two \ characters in my string - right? So there is no way I can do this "safely". >> 2.) Which characters do I need to escape for pgsql? Is ' the only one, >> and I need to escape it as '' ? Do I need to escape \ ? Will I >> need to >> escape all the characters that I escaped for MySQL? Where can I find >> out more? > > You need to escape ' and \ if you standard_conforming_strings is on. > Monitoring this setting can be tough, so the safest thing to do is > probably to always use the E'string' escape syntax and escape both > characters. I haven't found anything in the documentation about how this syntax works exactly. The documentation refers to "the E'...' syntax", but doesn't tell me what this syntax actually is (am I supposed to already know how this syntax works, so just need to be told to use it!?). Do I have to put the E in front of the beginning ', i.e. 'foo' becomes E'foo' ? (that can't be right, there must be some way I escape ' inside the string). So does 'foo' become 'E'f'E'o'E'o'' ? or what? How do I represent the literal string foo'bar\baz ? Thanks in advance, Tobias