加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 如何将字符串值转换为枚举

发布时间:2020-12-13 16:33:20 所属栏目:百科 来源:网络整理
导读:我有一个具有枚举类型的表,并且我创建了一个函数来向该表添加数据.我希望该函数在接受的方面慷慨,所以我采用一个文本作为枚举类型,并希望稍后再发表. 这是枚举: CREATE TYPE public.enum_log_priority AS ENUM ( 'critical','error','warning','notice','de
我有一个具有枚举类型的表,并且我创建了一个函数来向该表添加数据.我希望该函数在接受的方面慷慨,所以我采用一个文本作为枚举类型,并希望稍后再发表.

这是枚举:

CREATE TYPE public.enum_log_priority AS ENUM (
    'critical','error','warning','notice','debug'
);

这是功能:

CREATE OR REPLACE FUNCTION public.log_write(
    _message text,_priority text
) RETURNS integer AS
$body$
BEGIN
    _priority = lower(_priority);
    INSERT INTO log (message,priority) VALUES (_message,_priority);

    RETURN 0;
END
$body$
LANGUAGE 'plpgsql';

我知道这不行:

ERROR: column “priority” is of type enum_log_priority but expression is of type text

但我该怎么办?

插入时使用下面的语法
'critical'::enum_log_priority

请参阅一些链接

http://www.postgresql.org/docs/9.1/static/datatype-enum.html

Inserting into custom SQL types with prepared statements in java

java enum and postgresql enum

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读