473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

inserting an apostrophe

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();
....

i get the following error:

java.sql.SQLException: ERROR: parser: parse error at or near "NEIL" at character 47

at org.postgresql.core.QueryExecutor.execute(QueryExe cutor.java:94)
at org.postgresql.Connection.ExecSQL(Connection.java: 398)
at org.postgresql.jdbc2.Statement.execute(Statement.j ava:130)
at org.postgresql.jdbc2.Statement.executeQuery(Statem ent.java:54)

i thought that if i use the setString() method of the prepared
statement, it would automatically escape the apostrophe.

it only seems to be doing it on INSERT. with UPDATE it seems to behave
as it should (escape the character).

if i enter the record from psql, it works fine.

i am using postgres 7.2 on a freebsd 4.3 platform and jdk1.3.1

Peter Choe

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 11 '05 #1
3 5761
Ron
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 (?,?)).

i get the following error:

java.sql.SQLException: ERROR: parser: parse error at or near "NEIL" at
character 47

at org.postgresql.core.QueryExecutor.execute(QueryExe cutor.java:94)
at org.postgresql.Connection.ExecSQL(Connection.java: 398)
at org.postgresql.jdbc2.Statement.execute(Statement.j ava:130)
at org.postgresql.jdbc2.Statement.executeQuery(Statem ent.java:54)

i thought that if i use the setString() method of the prepared
statement, it would automatically escape the apostrophe.

it only seems to be doing it on INSERT. with UPDATE it seems to behave
as it should (escape the character).

if i enter the record from psql, it works fine.

i am using postgres 7.2 on a freebsd 4.3 platform and jdk1.3.1

Peter Choe

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #2

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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #3

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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Sterling | last post by:
i'm using delphi 7 and have a query in which i'm trying to find names that have an apostrophe in them, i.e. "o'mally". my problem is that when i write my select statement i can't get the quotes...
13
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
1
by: congngo | last post by:
Hi all Every time I export a table into an excel spreadsheet. It has a leading apostrophe on every cells. This drive me nut. I have to do a work around by export table into a txt file than...
1
by: spacehopper_man | last post by:
hi - I am having "apostrophe in sql" problems ;) I am executing a stored procedure on SQL Server - and passing in a string parameter. the string has a single apostrophe in it. the call...
1
by: Rose | last post by:
Hi all, I'm trying to create a clickable link, but the pesky apostrophe is preventing the link from getting displayed properly. I'm displaying the contents of a folder (with contains the...
4
by: fredb | last post by:
I am trying to store a last name that contains an apostrophe (O'Merle). It appears that PHP is adding a '\' to the last name (O\'Merle). The back slash is causing a SQL error. The last name is...
1
AllusiveKitten
by: AllusiveKitten | last post by:
Hi Is there a possible way to insert a record that includes an Apostrophe? At the moment it is splitting the field into two as it see's the apostrophe as a separator. Thank you for your...
2
by: mister-Ed | last post by:
I have a datagrid, and when initializing my field variables, I need to double up apostrophes so they are accepted into SQL dbase. In the line below, i'm trying to do this with the Replace function,...
9
by: Thomas 'PointedEars' Lahn | last post by:
Jukka K. Korpela wrote: IBTD. For example, in English it is customary (and AIUI expected) to use the character that ’ represents should be used to delimit a quotation within direct speech...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.