PostgreSQL數據類型:二進制bytea及大對象oid類型

參考資料 html

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

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

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

二進制類型bytea的操做(在最大值內,有內存限制)
函數

Create table byteatable(id int,obj bytea);

①直接強制類型輸入:post

Insert into byteatable values(1, '123'::bytea);  //插入文本

②直接插入逃逸序列編碼

bytea 文本逃逸八進制spa

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

十進制數值 code

描述

輸入逃逸形式

例子

輸出形式

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

Insert into byteatable values(1, '''');  //插入一個單引號-‘

經過base64的encode編碼字符串

encode在線編碼器 http://www.opinionatedgeek.com/dotnet/tools/base64encode/

你好  編碼爲  5L2g5aW9

select encode('你好', 'base64');
-------------------------------
5L2g5aW9

Insert into byteatable values(1,decode(‘5L2g5aW9’,’base64’));//5L2g5aW9是【你好】編碼後的代碼

經過pg_read_binary_file()函數

Insert into byteatable values(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(filename text [, offset bigint, length bigint])

text

Return the contents of a text file

pg_read_binary_file(filename text [, offset bigint, length bigint])

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

BYTEA

OID

Max. allowed space 

1 GB

2 GB

Data access

As a whole

Stream-style

Storage

In defined table

In pg_largeobject system table

Data manipulation

Using SQL and escaping sequnces

Only within transaction block by special functions

Loading

Preload

On demand

 

將二進制類型導出到文件

highgo=# SELECT lo_export(txt, E'C:\\HighGo\\Database\\1.3.1\\aa.txt') FROM lyy.
clob1;
 lo_export
-----------
         1
(1 行記錄)
相關文章
相關標籤/搜索