如何将(文件)数据插入PostgreSQL bytea列?
发布时间:2020-12-13 16:20:01 所属栏目:百科 来源:网络整理
导读:这个问题不是关于bytea v.oy v.blobs v.大对象等. 我有一个包含主键整数字段和bytea字段的表.我想将数据输入bytea字段.据推测,这可以通过其中一种PL /语言来完成,我可能会考虑将来使用PL / Python来做这件事. 由于我还在测试和试验,我只想使用“标准”SQL语
这个问题不是关于bytea v.oy v.blobs v.大对象等.
我有一个包含主键整数字段和bytea字段的表.我想将数据输入bytea字段.据推测,这可以通过其中一种PL /语言来完成,我可能会考虑将来使用PL / Python来做这件事. 由于我还在测试和试验,我只想使用“标准”SQL语句从文件(在服务器上)插入数据.我知道只有在服务器上具有写权限的管理员才能以我想要的方式插入数据.我不关心这个阶段,因为用户目前不会插入bytea数据.我一直在搜索各种StackExchange网站,PostgreSQL档案馆和互联网,但一直未能找到答案. 编辑:从2008年开始的This讨论意味着我想做的事情是不可能的.那么如何使用bytea字段? 编辑:This 2005年的类似问题仍未得到答复. 解决:psycopg网站上提供的详细信息here提供了我用Python编写的解决方案的基础.也可以使用PL / Python将二进制数据插入到bytea列中.我不知道使用“纯”SQL是否可行.
作为超级用户:
create or replace function bytea_import(p_path text,p_result out bytea) language plpgsql as $$ declare l_oid oid; begin select lo_import(p_path) into l_oid; select lo_get(l_oid) INTO p_result; perform lo_unlink(l_oid); end;$$;
create or replace function bytea_import(p_path text,p_result out bytea) language plpgsql as $$ declare l_oid oid; r record; begin p_result := ''; select lo_import(p_path) into l_oid; for r in ( select data from pg_largeobject where loid = l_oid order by pageno ) loop p_result = p_result || r.data; end loop; perform lo_unlink(l_oid); end;$$; 然后: insert into my_table(bytea_data) select bytea_import('/my/file.name'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |