Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2703007

Browse files
committed
Fix example of how to escape data in psql backslash commands.
1 parentc6d3c1b commit2703007

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

‎doc/src/sgml/ref/psql-ref.sgml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.164 2006/05/3111:47:20 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.165 2006/05/3122:34:35 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2265,27 +2265,24 @@ testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
22652265
testdb=&gt; <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
22662266
testdb=&gt; <userinput>INSERT INTO my_table VALUES (:content);</userinput>
22672267
</programlisting>
2268-
Onepossibleproblem with this approach is that <filename>my_file.txt</filename>
2268+
One problem with this approach is that <filename>my_file.txt</filename>
22692269
might contain single quotes. These need to be escaped so that
22702270
they don't cause a syntax error when the second line is processed. This
22712271
could be done with the program <command>sed</command>:
22722272
<programlisting>
2273-
testdb=&gt; <userinput>\set content '''' `sed -e "s/'/\\\\''/g" &lt; my_file.txt` ''''</userinput>
2273+
testdb=&gt; <userinput>\set content '''' `sed -e "s/'/''/g" &lt; my_file.txt` ''''</userinput>
22742274
</programlisting>
2275-
Observe the correct number of backslashes (6)! It works
2276-
this way: After <application>psql</application> has parsed this
2277-
line, it passes <literal>sed -e "s/'/\\''/g" &lt; my_file.txt</literal>
2278-
to the shell. The shell will do its own thing inside the double
2279-
quotes and execute <command>sed</command> with the arguments
2280-
<literal>-e</literal> and <literal>s/'/''/g</literal>. When
2281-
<command>sed</command> parses this it will replace the two
2282-
backslashes with a single one and then do the substitution. Perhaps
2275+
If you are using non-standard-conforming strings then you'll also need
2276+
to double backslashes. This is a bit tricky:
2277+
<programlisting>
2278+
testdb=&gt; <userinput>\set content '''' `sed -e "s/'/''/g" -e 's/\\/\\\\/g' &lt; my_file.txt` ''''</userinput>
2279+
</programlisting>
2280+
Note the use of different shell quoting conventions so that neither
2281+
the single quote marks nor the backslashes are special to the shell.
2282+
Backslashes are still special to <command>sed</command>, however, so
2283+
we need to double them. (Perhaps
22832284
at one point you thought it was great that all Unix commands use the
2284-
same escape character. And this is ignoring the fact that you might
2285-
have to escape all backslashes as well because
2286-
<acronym>SQL</acronym> text constants are also subject to certain
2287-
interpretations. In that case you might be better off preparing the
2288-
file externally.
2285+
same escape character.)
22892286
</para>
22902287

22912288
<para>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp