Fix example of how to escape data in psql backslash commands.
This commit is contained in:
parent
c6d3c1b817
commit
2703007501
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.164 2006/05/31 11:47:20 momjian Exp $
|
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.165 2006/05/31 22:34:35 tgl Exp $
|
||||||
PostgreSQL documentation
|
PostgreSQL documentation
|
||||||
-->
|
-->
|
||||||
|
|
||||||
@ -2265,27 +2265,24 @@ testdb=> <userinput>SELECT * FROM :foo;</userinput>
|
|||||||
testdb=> <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
|
testdb=> <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
|
||||||
testdb=> <userinput>INSERT INTO my_table VALUES (:content);</userinput>
|
testdb=> <userinput>INSERT INTO my_table VALUES (:content);</userinput>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
One possible problem with this approach is that <filename>my_file.txt</filename>
|
One problem with this approach is that <filename>my_file.txt</filename>
|
||||||
might contain single quotes. These need to be escaped so that
|
might contain single quotes. These need to be escaped so that
|
||||||
they don't cause a syntax error when the second line is processed. This
|
they don't cause a syntax error when the second line is processed. This
|
||||||
could be done with the program <command>sed</command>:
|
could be done with the program <command>sed</command>:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
testdb=> <userinput>\set content '''' `sed -e "s/'/\\\\''/g" < my_file.txt` ''''</userinput>
|
testdb=> <userinput>\set content '''' `sed -e "s/'/''/g" < my_file.txt` ''''</userinput>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
Observe the correct number of backslashes (6)! It works
|
If you are using non-standard-conforming strings then you'll also need
|
||||||
this way: After <application>psql</application> has parsed this
|
to double backslashes. This is a bit tricky:
|
||||||
line, it passes <literal>sed -e "s/'/\\''/g" < my_file.txt</literal>
|
<programlisting>
|
||||||
to the shell. The shell will do its own thing inside the double
|
testdb=> <userinput>\set content '''' `sed -e "s/'/''/g" -e 's/\\/\\\\/g' < my_file.txt` ''''</userinput>
|
||||||
quotes and execute <command>sed</command> with the arguments
|
</programlisting>
|
||||||
<literal>-e</literal> and <literal>s/'/''/g</literal>. When
|
Note the use of different shell quoting conventions so that neither
|
||||||
<command>sed</command> parses this it will replace the two
|
the single quote marks nor the backslashes are special to the shell.
|
||||||
backslashes with a single one and then do the substitution. Perhaps
|
Backslashes are still special to <command>sed</command>, however, so
|
||||||
|
we need to double them. (Perhaps
|
||||||
at one point you thought it was great that all Unix commands use the
|
at one point you thought it was great that all Unix commands use the
|
||||||
same escape character. And this is ignoring the fact that you might
|
same escape character.)
|
||||||
have to escape all backslashes as well because
|
|
||||||
<acronym>SQL</acronym> text constants are also subject to certain
|
|
||||||
interpretations. In that case you might be better off preparing the
|
|
||||||
file externally.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user