|
1 | 1 | <!--
|
2 |
| -$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.164 2006/05/31 11:47:20 momjian Exp $ |
| 2 | +$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.165 2006/05/31 22:34:35 tgl Exp $ |
3 | 3 | PostgreSQL documentation
|
4 | 4 | -->
|
5 | 5 |
|
@@ -2265,27 +2265,24 @@ testdb=> <userinput>SELECT * FROM :foo;</userinput>
|
2265 | 2265 | testdb=> <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
|
2266 | 2266 | testdb=> <userinput>INSERT INTO my_table VALUES (:content);</userinput>
|
2267 | 2267 | </programlisting>
|
2268 |
| - One possible problem with this approach is that <filename>my_file.txt</filename> |
| 2268 | + One problem with this approach is that <filename>my_file.txt</filename> |
2269 | 2269 | might contain single quotes. These need to be escaped so that
|
2270 | 2270 | they don't cause a syntax error when the second line is processed. This
|
2271 | 2271 | could be done with the program <command>sed</command>:
|
2272 | 2272 | <programlisting>
|
2273 |
| -testdb=> <userinput>\set content '''' `sed -e "s/'/\\\\''/g" < my_file.txt` ''''</userinput> |
| 2273 | +testdb=> <userinput>\set content '''' `sed -e "s/'/''/g" < my_file.txt` ''''</userinput> |
2274 | 2274 | </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" < 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=> <userinput>\set content '''' `sed -e "s/'/''/g" -e 's/\\/\\\\/g' < 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 |
2283 | 2284 | 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.) |
2289 | 2286 | </para>
|
2290 | 2287 |
|
2291 | 2288 | <para>
|
|
0 commit comments