#navi(../)
* ERROR: invalid escape stringの対処法 [#xbf56251]

#contents
#htmlinsertpcsp(db-top.html,db-sp.html)

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..)



* 解決方法 [#a719f866]
postgresql.confの設定を以下の値にして回避することできる。
 standard_conforming_strings = off

* 調査結果 [#uaa4e56a]
PostgreSQL9.1から、postgresql.confファイルの standard_conforming_strings
の値のデフォルト値がonに変更されたため。

* standard_conforming_strings = on/off のときの動作確認 [#v2ac9c64]
以下にstandard_conforming_stringsがonの時とoffの時の動作例を2つ記述する。

** テストその1(\\でテストしてみる) [#wadd02e1]

 $ 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で表示している。~
エスケープされるので、実際はabc\\になる。~
standard_conforming_strings = offなので、そのままの表示であるabc\\が表示された。

 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で表示している。~
エスケープされるので、実際はabc\\になる。~
standard_conforming_strings = onなので、エスケープ文字である\を付加したabc\\\\が表示された。

** テストその2(\nをいれてみてテスト) [#v2151e20]
以下は改行である\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'\\'.を非表示にする方法 [#c8977695]
以下のメッセージは、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

* 参考ページ [#i8880f42]
- Let's postgres PostgreSQL 9.1の新機能~
http://lets.postgresql.jp/documents/technical/9.1/
- 18.13. バージョンとプラットフォーム互換性~
http://www.postgresql.jp/document/9.1/html/runtime-config-compatible.html
- 18.13. Version and Platform Compatibility~
http://www.postgresql.org/docs/9.1/static/runtime-config-compatible.html
- Standard conforming strings~
http://wiki.postgresql.org/wiki/Standard_conforming_strings


#htmlinsertpcsp(db-btm.html,db-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS