在Postgresql数据库中插入图像 [英] Insert an image in postgresql database

查看:179
本文介绍了在Postgresql数据库中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将图像 bytea插入到postgreSql数据库的表中?我在论坛上搜索了几个小时,看到相同的问题已发布数十次,但仍未找到一个答案。我所看到的只是如何将.jpeg插入到我不需要的旧列中。

I would like to know How can I insert an image "bytea" into a table of my postgreSql database? I've been searching forums for hours and have seen the same question posted dozens of times, but yet to find a single answer. All I see is how to insert .jpeg's into an old column which isn't what I need.

这是数据库表:

create table category  (
"id_category" SERIAL,
"category_name" TEXT,
"category_image" bytea,
constraint id_cat_pkey primary key ("id_category"))without oids;

当我添加新行时,它不起作用:

and when I add a new line, it doesn't work :

insert into category(category_name,category_image) values('tablette', lo_import('D:\image.jpg'));


推荐答案

insert into category(category_name,category_image) values('tablette', bytea('D:\image.jpg'));

如果列类型为bytea,则上述解决方案有效

The above solution works if column type is bytea

insert into category(category_name,category_image) values('tablette', lo_import('D:\image.jpg'));

如果列类型为oid,即Blob,则上述解决方案有效

The above solution works if column type is oid i.e., Blob

insert into category(category_name,category_image) values('tablette',decode('HexStringOfImage',hex));

上述解码函数带有两个参数。第一个参数是Image的HexString,第二个参数默认为hex。解码功能将hexString转换为字节,并存储在postgres的bytea数据类型列中。

The above decode function take two parameters. First parameter is HexString of Image.The second parameter is hex by default.Decode function coverts the hexString to bytes and store in bytea datatype column in postgres.

这篇关于在Postgresql数据库中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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