ERROR: invalid escape stringの対処法 †PostgreSQL9.1を利用し、あるオープンソースソリューションのバックエンドとして利用したとき、以下のエラーが発生した。 SELECT xxxxx WHERE xxxxx."xxxxx" = ? AND (xxxxx LIKE ? ESCAPE '\\' ) ORDER BY xxxxx ERROR: invalid escape string HINT: Escape string must be empty or one character..) 解決方法 †postgresql.confの設定を以下の値にして回避することできる。 standard_conforming_strings = off 調査結果 †PostgreSQL9.1から、postgresql.confファイルの standard_conforming_strings の値のデフォルト値がonに変更されたため。 standard_conforming_strings = on/off のときの動作確認 †以下にstandard_conforming_stringsがonの時とoffの時の動作例を2つ記述する。 テストその1(\\でテストしてみる) †$ psql template1 psql (9.1.0) Type "help" for help. template1=# show standard_conforming_strings; standard_conforming_strings ----------------------------- off (1 row) template1=# select 'abc\\\\'; WARNING: nonstandard use of \\ in a string literal LINE 1: select 'abc\\\\'; ^ HINT: Use the escape string syntax for backslashes, e.g., E'\\'. ?column? ---------- abc\\ (1 row) 上記の実行例では、abc\\\\をselectで表示している。 template1=# set standard_conforming_strings=on; SET template1=# show standard_conforming_strings; standard_conforming_strings ----------------------------- on (1 row) template1=# select 'abc\\\\'; ?column? ---------- abc\\\\ (1 row) 上記の実行例では、abc\\\\をselectで表示している。 テストその2(\nをいれてみてテスト) †以下は改行である\nを利用してテストした例。 template1=# set standard_conforming_strings=on; SET template1=# select 'hello\nworld\n'; ?column? ---------------- hello\nworld\n (1 row) template1=# set standard_conforming_strings=off; SET template1=# select 'hello\nworld\n'; WARNING: nonstandard use of escape in a string literal LINE 1: select 'hello\nworld\n'; ^ HINT: Use the escape string syntax for escapes, e.g., E'\r\n'. ?column? ---------- hello + world + (1 row) HINT: Use the escape string syntax for backslashes, e.g., E'\\'.を非表示にする方法 †以下のメッセージは、postgresql.confのescape_string_warningをoffにすると出力されなくなる。 HINT: Use the escape string syntax for backslashes, e.g., E'\\'. postgrsql.conf内のescape_string_warningをoffに設定すること escape_string_warning = off 参考ページ †
|