问题
I am trying to do the following:
UPDATE bodycontent set body=CONTENT_FROM_FILE where contentid=12943363;
I tried the following based on the highest voted answer to this question.
\set contentfill `cat Foo.txt`
UPDATE bodycontent set body=:'contentfill' where contentid=12943363;
This results in the following error.
ERROR: syntax error at or near ":"
LINE 1: UPDATE bodycontent set body=:'contentfill' where contentid=1...
Is there a clean, simple and effective way to achieve this on the psql command line?
Here is the output of psql --version:
psql (PostgreSQL) 8.4.17
回答1:
After much searching, I discovered this answer, which does not directly discuss the problem of reading files, but gave me the necessary ingredient to make this work for my ancient Postgres.
\set contentFill `cat Foo.txt`
\set quoted_contentFill '\'' :contentFill '\''
UPDATE bodycontent SET body=:quoted_contentFill WHERE contentid=12943363;
Naturally, this will fail if there are un-escaped quotes inside Foo.txt, but I can easily preprocess to ensure there are none.
来源:https://stackoverflow.com/questions/23259314/how-can-i-update-a-single-field-in-psql-with-the-contents-of-a-file