Reputation: 7685
I'm inserting text from a Java application into a Postgresql database, but it all crashes when a ' char is encountered in the String. I've tried using replaceAll(" ' ", " \\' "); even diffrent variants of it with more \ chars, yet it still puts a single ' in the String without the escape sign.
Is there any way of replacing the ' with an \' in the String? Or another way of putting Strings containig single quotes into Postgresql?
Upvotes: 2
Views: 10080
Reputation: 27561
Most SQL implementations use '' (2 single quotes) to escape a single quote.
Example:
SELECT * FROM users WHERE f_name='foo''bar';
Or you could use double dollar sign:
Example:
SELECT * FROM users WHERE f_name=$$foo''bar$$;
Both statements would search for the string foo'bar
Upvotes: 2
Reputation: 284786
You shouldn't have to worry about doing this manually if you're using prepared statements properly.
Upvotes: 15