Sar*_*rra 11 sql database postgresql
我想知道如何将图像"bytea"插入到postgreSql数据库的表中?我一直在搜索论坛几个小时,并且已经看过几十次同样的问题,但还没找到一个答案.我只看到如何将.jpeg插入到oid列中,这不是我需要的.
这是数据库表:
create table category (
"id_category" SERIAL,
"category_name" TEXT,
"category_image" bytea,
constraint id_cat_pkey primary key ("id_category"))without oids;
Run Code Online (Sandbox Code Playgroud)
当我添加一个新行时,它不起作用:
insert into category(category_name,category_image) values('tablette', lo_import('D:\image.jpg'));
Run Code Online (Sandbox Code Playgroud)
小智 7
如果列类型是bytea那么你可以简单地使用' pg_read_binary_file '。
例子:pg_read_binary_file('/path-to-image/')
检查pg_read_binary_file的 postgresql 文档
insert into category(category_name,category_image) values('tablette', bytea('D:\image.jpg'));
Run Code Online (Sandbox Code Playgroud)
如果列类型为bytea,则上述解决方案有效
insert into category(category_name,category_image) values('tablette', lo_import('D:\image.jpg'));
Run Code Online (Sandbox Code Playgroud)
如果列类型是oid,即Blob,则上述解决方案有效
insert into category(category_name,category_image) values('tablette',decode('HexStringOfImage',hex));
Run Code Online (Sandbox Code Playgroud)
上述解码功能有两个参数.第一个参数是Image的HexString.默认情况下,第二个参数是十六进制.Decode函数将hexString转换为字节并存储在postgres中的bytea数据类型列中.