このエントリーをはてなブックマークに追加


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で表示している。
エスケープされるので、実際は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をいれてみてテスト)

以下は改行である\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

参考ページ


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-03-20 (金) 22:08:00