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

查看:117
本文介绍了在 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.Decode函数将hexString转换为bytes,存储在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天全站免登陆