| « Force Windows XP to Update Microsoft Products | IMAP Account in Outlook Not Deleting Mail Items » |
Apostrophe in a SQL Query String
I was running into a problem with a SQL query string being terminated to soon. One of the variables I was using in the concatenation of the string contained an apostrophe. In order to handle this situation you have to put two apostrophes to escape the character in the string.
For example SELECT column1 FROM table WHERE column2='values's' needs to be SELECT column1 FROM table WHERE column2='values''s'.
The application I was writing used Jython. Jython is just Python implemented in Java. I have found working with strings in Jython to be very easy.
Here is the function I came up with to use on the variable before using it in the SQL query string.
def sqlApostrophes(strInput):
strOutput = strInput.replace("'","''")
return strOutput
Feedback awaiting moderation
This post has 5 feedbacks awaiting moderation...