aspose file tools
The moose likes Oracle/OAS and the fly likes Insert query with apostrophe Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Products » Oracle/OAS
Reply Bookmark "Insert query with apostrophe" Watch "Insert query with apostrophe" New topic
Author

Insert query with apostrophe

Sneha Neil
Greenhorn

Joined: May 31, 2004
Posts: 5
Hi,

I want to insert some values into a table. These values might contain apostrophe which will throw an error while inserting.

I cannot do the following:
insert into xx values('how''s u?')
where the value is hardcoded and so the apostrophe can be escaped.

I have to do something like
insert into xx values(someval)
where someval might contain an apostrophe in any position.

Can someone please help me in finding a solution?
Thanks!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

In your code you will need to create a method that takes a string, checks if it has an apostrophe, then get the index and place an escape character in front of it so that you won't get the error.

This is not just a Java issue, I had to do the same type of thing in a VB 6.0 application.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Sneha Neil,

Are you using Java/JDBC? If you are then just use a PreparedStatement rather than a plain old Statement and all your worries go away!

Maybe I should give an example. Instead of the following (which will break):

Use the following:

The JDBC driver knows that you're supplying a String parameter and takes care of any escaping required for you.

If you're not using Java, what technology are you using with Oracle?

Jules
Sneha Neil
Greenhorn

Joined: May 31, 2004
Posts: 5
Hi,
Sorry for the late response.The different time zones are to blame!

I'm not using Java technology here. I'm using a workflow tool called PEGA which calls Oracle. Since database interactions are not very efficient through PEGA,we directly called the insert query without handling it through any method as suggested by Mark.

Since we want the inserts to be done as a background process without any user interaction, we are facing this problem.We will now have to handle it through scripting in the front end. This defeats our purpose but there doesn't seem to be any workaround.

Thanks a lot for all your help!
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1112

Sneha,
In SQL, string literals are delimited by apostrophes (otherwise known as single quotes). In order to use an apostrophe as part of your string literal, you simply write it twice, for example:

Good Luck,
Avi.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26168
    
  66

Avi's example jogged my memory a bit. There is a problem in Oracle (or at least in sql plus) where the '' is only recognized if it is the last character in a string. I had to concatenate the string up to and including the quote with a string containing the rest.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert query with apostrophe
 
Similar Threads
URLEncoder class ?
Closing connection and resultset?
Prepared Statement with wild card
The host is unknown to the system
inserting string values with an apostrophe