二進制字符串
| 名字 | 存儲尺寸 | 描述 |
|---|---|---|
bytea |
1或4字節(jié)外加真正的二進制串 | 變長二進制串 |
db=# create table demo_bytea(bytea bytea);
插入
插入文本
db=# insert into demo_bytea values('123');
INSERT 0 1
db=# select * from demo_bytea;
bytea
----------
\x313233
(1 row)
插入轉(zhuǎn)義類型
這個不是很理解,什么叫轉(zhuǎn)義類型,我的理解,就是將二進制的表示為八進制或者十六進制這種字符串,為什么這么表示,主要是有一些特殊的字符比如\b\n\t,這種需要轉(zhuǎn)義的字符,如果不轉(zhuǎn)義,則作為字符串添加進去的話,就會有問題,所以才需要通過八進制或者十六進制的方式進行表示(因為直接用二進制太長了,雖然也是可以)。插入的時候怎么使用呢
插入八進制數(shù)據(jù)
db=# insert into demo_bytea values('\001');
INSERT 0 1
db=# insert into demo_bytea values('\002');
INSERT 0 1
db=# insert into demo_bytea values('\003');
INSERT 0 1
db=# select * from demo_bytea;
bytea
-------
\x01
\x02
\x03
(3 rows)
db=# insert into demo_bytea values('\001');
INSERT 0 1
db=# insert into demo_bytea values('\001\002');
INSERT 0 1
db=# insert into demo_bytea values('\001\002\032');
INSERT 0 1
db=# insert into demo_bytea values('\001\002\032\101');
INSERT 0 1
db=# insert into demo_bytea values('\001\002\032\101\021');
INSERT 0 1
db=# select * from demo_bytea;
bytea
--------------
\x01
\x0102
\x01021a
\x01021a41
\x01021a4111
(5 rows)
插入十六進制數(shù)據(jù)
db=# insert into demo_bytea values(E'\x01');
INSERT 0 1
db=# insert into demo_bytea values(E'\x02');
INSERT 0 1
db=# insert into demo_bytea values(E'\x03');
INSERT 0 1
db=# insert into demo_bytea values(E'\x04');
INSERT 0 1
db=# insert into demo_bytea values(E'\x05');
INSERT 0 1
db=# select * from demo_bytea;
bytea
-------
\x01
\x02
\x03
\x04
\x05
base64編碼
通過base64編碼將數(shù)據(jù)插入進去
db=# select encode('你好', 'base64');
encode
----------
5L2g5aW9
(1 row)
db=# insert into demo_bytea values(decode('5L2g5aW9', 'base64'));
INSERT 0 1
db=# select * from demo_bytea;
bytea
----------------
\xe4bda0e5a5bd
(1 row)
本地路徑
將本地的文件插入到pg數(shù)據(jù)庫中,通過函數(shù)
| 函數(shù) | 返回類型 | 描述 |
|---|---|---|
| pg_read_file(filename text [, offset bigint, length bigint]) | text | 返回文件內(nèi)容為text格式 |
| pg_read_binary_file(filename text [, offset bigint, length bigint]) | bytea | 返回文件內(nèi)容為bytea格式 |
文本方式讀入
注意如果采用文本,則需要強制轉(zhuǎn)換,否則會失敗
db=# insert into demo_bytea values(pg_read_file('/var/file/test.txt')::bytea);
INSERT 0 1
db=# select * from demo_bytea;
bytea
--------
\x320a
(1 row)
不轉(zhuǎn)換的話會失敗
db=# insert into demo_bytea values(pg_read_file('/var/file/test.txt'))
db-# ;
ERROR: column "bytea" is of type bytea but expression is of type text
LINE 1: insert into demo_bytea values(pg_read_file('/var/file/test.t...
^
HINT: You will need to rewrite or cast the expression.
二進制文本讀入
db=# create table demo_time(time time);
CREATE TABLE
db=# insert into demo_bytea values(pg_read_binary_file('/var/file/test.txt'));
INSERT 0 1
db=# select * from demo_bytea;
bytea
--------
\x320a
(1 row)
其中文件的內(nèi)容為
# cat test.txt
2
讀取
后端代碼中進行讀取,這里用到自己的Orm框架Neo()
@Test
public void test3(){
String url="jdbc:postgresql://localhost:54321/db";
String user = "postgres";
String password = "pg.123";
Neo db = Neo.connect(url, user, password);
ByteaDO result = db.one(ByteaDO.class, "demo_bytea", NeoMap.of());
// 320a
show(HexUtils.toHexString(result.getBytea()));
// 2
show(new String(result.getBytea()));
}
參考:
官網(wǎng)
http://postgres.cn/docs/11/datatype-binary.html
其他
https://blog.csdn.net/xinpo66/article/details/18961427