2

i am using Postgresql Db. i got this error for using ' on a text,

unterminated quoted string at or near "'\'')"

it is my function call:

SELECT "public"."sendMessage"(12152015, 12584, 1258487, 13960614093221020, '\'')

how can i insert these characters into db without any problem??

Saeed
  • 5,413
  • 3
  • 26
  • 40
  • This is also documented in the manual: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS –  Sep 05 '17 at 09:51

1 Answers1

3

The right way to quote a single quote is to repeat it, not to escape it:

SELECT "public"."sendMessage"(12152015, 12584, 1258487, 13960614093221020, '''')
                                                                            ^^
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • and what about double quotation and other special characters user can enter?? – Saeed Sep 05 '17 at 09:39
  • @Saeed.At Double quotes are not special characters in a string literal. – Mark Rotteveel Sep 05 '17 at 09:40
  • Single quote is the only character that you have to escape. It's better to use parameterized queries and have the database library do this for you. – Andomar Sep 05 '17 at 09:40
  • 1
    @Saeed.At: see the manual for details: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS –  Sep 05 '17 at 09:51
  • `Also, a single quote can be included in an escape string by writing \', in addition to the normal way of ''.`@a_horse_with_no_name , but it does not true, is it? – Saeed Sep 05 '17 at 10:02