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

前端之家收集整理的这篇文章主要介绍了PostgreSQL数据类型:二进制bytea及大对象oid类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参考资料

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'\\000'

SELECT E'\\000'::bytea;

\000

39

单引号

'''' E'\\047'

SELECT E'\''::bytea;

'

92

反斜杠

E'\\\\' E'\\134'

SELECT E'\\\\'::bytea;

\\

0 31 以及 127 255

"不可打印"字节

E'\\xxx'(八进制值)

SELECT E'\\001'::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:\\HighGo\\Database\\1.3.1\\aa.txt')FROMlyy.
clob1;
lo_export
-----------
1
(1行记录)
原文链接:https://www.f2er.com/postgresql/196126.html

猜你在找的Postgre SQL相关文章