How can I update a single field in psql with the contents of a file?

拥有回忆 提交于 2020-01-03 05:43:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!