如何使用文件内容更新psql中的单个字段? [英] How can I update a single field in psql with the contents of a file?

查看:116
本文介绍了如何使用文件内容更新psql中的单个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I am trying to do the following:

UPDATE bodycontent set body=CONTENT_FROM_FILE where contentid=12943363;

我根据对此问题

\set contentfill `cat Foo.txt`
UPDATE bodycontent set body=:'contentfill' where contentid=12943363;

这会导致以下错误。

ERROR:  syntax error at or near ":"                                                  
LINE 1: UPDATE bodycontent set body=:'contentfill' where contentid=1...              

psql 上是否有一种干净,简单且有效的方法来实现这一目标命令行?

Is there a clean, simple and effective way to achieve this on the psql command line?

这是 psql --version 的输出:

psql (PostgreSQL) 8.4.17 


推荐答案

经过大量搜索,我发现了此答案,它没有直接讨论阅读问题文件,但为我的古代 Postgres 提供了必要的成分。

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; 

自然,如果 Foo中有未转义的引号,则此操作将失败。 txt ,但是我可以轻松地进行预处理以确保没有任何内容。

Naturally, this will fail if there are un-escaped quotes inside Foo.txt, but I can easily preprocess to ensure there are none.

这篇关于如何使用文件内容更新psql中的单个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆