Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何使用字符串更新文本[]字段_Postgresql - Fatal编程技术网

Postgresql 如何使用字符串更新文本[]字段

Postgresql 如何使用字符串更新文本[]字段,postgresql,Postgresql,我有一个表,其中有一个名为tags的字段,可以包含任意数量的字符串: Table "public.page" Column | Type | Modifiers ----------------------+--------------------------+---------------------------------- tags

我有一个表,其中有一个名为tags的字段,可以包含任意数量的字符串:

                                Table "public.page"
        Column        |           Type           |            Modifiers
----------------------+--------------------------+----------------------------------
 tags                 | text[]                   | not null default ARRAY[]::text[]
我想在tags字段中添加一个字符串,但我似乎无法让concat函数为我工作。我试过:

update page set tags=concat('My New String',tags);
ERROR:  function concat(unknown, text[]) does not exist
LINE 1: update page set tags=concat('My New String',tags) where ...
                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.


有什么想法吗?

你有没有尝试将
|
连接起来

select array['abc','def']::text[] || 'qwerty'::text;


注:此答案是对OP原始(未编辑)问题的回答。其他答案包含与更新问题相关的更多细节。

在PostgreSQL的类型系统中,文本
'My New String'
不是
varchar
text
值,而是类型
未知的文本,可以作为任何类型处理。(例如,
日期
的文字可以是
'2013-08-29'
;这不会被处理为
varchar
,然后转换为
日期
,它将被解释为非常低级别的“
日期
文字”)

通常,PostgreSQL可以自动推断类型,但如果不能,则需要使用以下命令之一告诉它您希望将文本视为
文本

  • 文本“我的新字符串”
    (SQL标准文字语法)
  • Cast('My New String'作为文本)
    (SQL标准Cast语法,但在此上下文中不是真正的Cast)
  • “我的新字符串”:text
    (PostgreSQL非标准强制转换语法,但可读性很强)
在您的例子中,错误消息
运算符不是唯一的:unknown | | text[]
表示Postgres可以将文本解释为多种类型,每种类型都有自己的
|
运算符定义

因此,您需要这样的内容(我删除了不必要的括号):


字符串“no luck”不是PostgreSQL中的内置错误消息。
|
运算符应该可以工作。您收到了什么错误消息?嗯,出于某种原因,它不理解
“我的新字符串”
应该是文本。尝试将其转换为这样。或者,使用适当的函数
array\u prepend
array\u append
,而不是
concat
,这并不适用于数组。好的-成功了-它使用该语法进行了更新(非常感谢您的详细回答-我正在学习!)在这里用“引用”值解释类型问题是一个启示,它节省了我数小时的时间。非常感谢。是的-谢谢你的输入-对不起,我没有提前给你所有的信息!
select array['abc','def']::text[] || 'qwerty'::text;
update page set tags = 'My New String'::text || tags;