|
1 | 1 | <!-- |
2 | | -$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.162 2006/05/26 19:51:29 tgl Exp $ |
| 2 | +$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.163 2006/05/31 11:35:17 momjian Exp $ |
3 | 3 | PostgreSQL documentation |
4 | 4 | --> |
5 | 5 |
|
@@ -2262,22 +2262,22 @@ testdb=> <userinput>SELECT * FROM :foo;</userinput> |
2262 | 2262 | copy the contents of a file into a table column. First load the file into a |
2263 | 2263 | variable and then proceed as above. |
2264 | 2264 | <programlisting> |
2265 | | -testdb=> <userinput>\set content '\'' `cat my_file.txt` '\''</userinput> |
| 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 | 2268 | One possible 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 | 2275 | Observe the correct number of backslashes (6)! It works |
2276 | 2276 | this way: After <application>psql</application> has parsed this |
2277 | | - line, it passes <literal>sed -e "s/'/\\\'/g" < my_file.txt</literal> |
| 2277 | + line, it passes <literal>sed -e "s/'/\\''/g" < my_file.txt</literal> |
2278 | 2278 | to the shell. The shell will do its own thing inside the double |
2279 | 2279 | quotes and execute <command>sed</command> with the arguments |
2280 | | - <literal>-e</literal> and <literal>s/'/\\'/g</literal>. When |
| 2280 | + <literal>-e</literal> and <literal>s/'/''/g</literal>. When |
2281 | 2281 | <command>sed</command> parses this it will replace the two |
2282 | 2282 | backslashes with a single one and then do the substitution. Perhaps |
2283 | 2283 | at one point you thought it was great that all Unix commands use the |
|