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

PostgreSQL数据类型:二进制bytea及大对象oid类型

发布时间:2020-12-13 17:36:39 所属栏目:百科 来源:网络整理
导读:参考资料 PostgreSQL Doc:http://www.postgresql.org/docs/9.2/static/datatype-binary.html PostgreSQL public API(LargeObject): http://jdbc.postgresql.org/documentation/publicapi/index.html PostgreSQL JDBC Interface: http://jdbc.postgresql.org/

参考资料

PostgreSQL Doc:http://www.postgresql.org/docs/9.2/static/datatype-binary.html

PostgreSQL public API(LargeObject): http://jdbc.postgresql.org/documentation/publicapi/index.html

PostgreSQL JDBC Interface: http://jdbc.postgresql.org/documentation/head/binary-data.html

二进制类型bytea的操作(在最大值内,有内存限制)

Createtablebyteatable(idint,objbytea);

①直接强制类型输入:

Insertintobyteatablevalues(1,'123'::bytea);//插入文本

②直接插入逃逸序列

bytea 文本逃逸八进制

http://www.postgresql.org/docs/9.2/interactive/datatype-binary.html#DATATYPE-BINARY-TABLE

十进制数值

描述

输入逃逸形式

例子

输出形式

0

八进制的零

E'00'

SELECT E'00'::bytea;

00

39

单引号

'''' E'47'

SELECT E''':bytea;

'

92

反斜杠

E'' E'134'

SELECT E'':bytea;

0 31 以及 127 255

"不可打印"字节

E'xxx'(八进制值)

SELECT E'01':bytea;

001

Insertintobyteatablevalues(1,'''');//插入一个单引号-‘

通过base64的encode编码字符串

encode在线编码器 http://www.opinionatedgeek.com/dotnet/tools/base64encode/

你好 编码为 5L2g5aW9

selectencode('你好','base64');
-------------------------------
5L2g5aW9

Insertintobyteatablevalues(1,decode(‘5L2g5aW9’,’base64’));//5L2g5aW9是【你好】编码后的代码

通过pg_read_binary_file()函数

Insertintobyteatablevalues(256,pg_read_binary_file('lob/imagejpg'));//插入一张图片-../data/lob/image.jpg

注意:函数pg_read_binary_file()中的路径必须是相对路径,默认路径是data目录下,并且必须在data目录下或者data目录的子目录下。

Name

Return Type

Description

pg_read_file(filenametext[,offsetbigint,lengthbigint])

text

Return the contents of a text file

pg_read_binary_file(filenametext[,lengthbigint])

bytea

Return the contents of a file

⑤通过copy to可以导出bytea的文本形式编码 ,通过copy from可以通过文本形式的编码想bytea导入二进制对象。



大对象类型oid的操作(在最大值内,没有内存限制)

Create table oidtable(id int,obj oid);
Insert into oidtable(id,obj) values(1,lo_import(‘d:/1.jpg’));
select lo_export(obj,‘e:/11.jpg’) from oidtable where id=1; //将以上插入的1.jp从表中取出来存成e盘的11.jpg。

大对象数据在pg_largeobject表中是以其创建时的OID标识的。每个大对象都分解成足够小的小段或者"页面"以便以行的形式存储在pg_largeobject 里。每页的数据定义为LOBLKSIZE(目前是BLCKSZ/4 或者通常是 2K 字)。


Byteaoid的比较

资料:http://www.microolap.com/products/connectivity/postgresdac/help/TipsAndTricks/ByteaVsOid.htm

BYTEA vs OID (Large Objects)

Comparative table

Characteristic

BYTEAOID

Max. allowed space

1 GB

2 GB

Data access

As a whole

Stream-style

StorageIn defined tableIn pg_largeobject system tableData manipulationUsing SQL and escaping sequncesOnly within transaction block by special functionsLoadingPreloadOn demand

将二进制类型导出到文件

highgo=#SELECTlo_export(txt,E'C:HighGoDatabase1.3.1aa.txt')FROMlyy.
clob1;
lo_export
-----------
1
(1行记录)

(编辑:李大同)

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

    推荐文章
      热点阅读