Re: inserting an apostrophe

From: Scott Lamb <slamb(at)slamb(dot)org>
To: Ron <rstpierre(at)syscor(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: inserting an apostrophe
Date: 2003-09-10 05:49:01
Message-ID: 7AC0477B-E352-11D7-A19E-000A95891440@slamb.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Tuesday, Sep 9, 2003, at 10:28 America/Chicago, Ron wrote:

> Peter Choe wrote:
>> i am trying to add a record of a name. the name has an apostrophe
>> (O'Neil).
>> i am doing this through a servlet and wrote the code to use
>> PreparedStatement.
>> when i do:
>> ...
>> PrepareStatement pstmt = con.prepareStatement("INSERT INTO directory
>> (name, phone) VALUES (?,?)");
>> pstmt.setString(1, name);
>> pstmt.setString(2, phone);
>> pstmt.executeUpdate();
>> ...
> We do this all the time (pg 7.3, java 1.4) without any problems. The
> only problem I see with this snippet is the lack of a terminating
> semicolon on your insert statement (after (?,?)).

None is necessary; that's a statement separator and it's only expecting
one statement. (In fact, some databases' JDBC and/or ODBC drivers fail
if you _do_ put in a semicolon.)

Peter, I see nothing wrong with that code at all. And I've also
inserted many records containing apostrophes without problems. What
version of the JDBC drivers are you using? In particular, the ones
shipped with the database aren't necessarily the best available, even
at the time that version of the database was released. Try downloading
the freshest ones from http://jdbc.postgresql.org/.

Scott

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-09-10 06:27:45 Re: Find overlapping time intervals, how?
Previous Message Jan Wieck 2003-09-10 05:31:59 Re: Replaceing records