如何向postgreSQL中添加bytea类型的大对象数据

如何向postgreSQL中添加bytea类型的⼤对象数据
也欢迎⼤家转载本篇⽂章。分享知识,造福⼈民,实现我们中华民族伟⼤复兴!
1)请教⼤家,如何向postgreSQL中添加bytea类型的⼤对象数据?
和mysql的⼀样不,需要bind吗,那对应mysql_stmt_init和mysql_stmt_prepare、mysql_stmt_bind_param、mysql_stmt_execute 对API是哪些 楼主 – guojinshihuaidan:
1个回复
回复 1楼 – guojinshihuaidan:添加成功了,⽤PQprepare和PQexecPrepared
2)
如果想要在PostgreSQL中存储⼆进制数据,例如存储Word、Excel⽂档,图⽚⽂件等,可以使⽤bytea类型的列。bytea类型是PostgreSQL特有的存储⼆进制数据的字段类型,与SQL标准中的BLOB和BINARY LARGE OBJECT类型异曲同⼯。这在PostgreSQL⽂档的bytea类型介绍中有所说明。
接下来先说说如何向表中插⼊、更新bytea数据。
PostgreSQL允许在SQL命令中包含bytea类型的数据,以便能够使⽤INSERT向表中插⼊含有⼆进制数据的记录,使⽤UPDATE和调⽤与bytea类型相关的函数更新和操作bytea类型数据。⼆进制数据是⼀个字节序列,然⽽SQL命令是⽂本字符串,怎样在SQL中写⼊⼆进制数据呢?答案很简单,把每⼀个字节转换成对应的三位⼗进制数字的⼋进制数字符串表⽰,以双斜线做为前缀,即0x00表⽰为\\000、
0x2C表⽰为\\02C、0xFF表⽰为\\377,并按照bytea类型的要求在字符串前端的单引号外注明E。举例如下:
INSERT INTO table1 (fileid, filename, content) VALUES (1, 'filename.doc', E'\\000\\001\\002');
INSERT INTO table1 (fileid, filename, content) VALUES (2, 'anotherfile.jpg', E'\\000\\377');
UPDATE table1 SET content = E'\\000\\000\\000' WHERE fileid = 1;
UPDATE table1 SET content = content || E'\\377\\377\\377' WHERE fileid = 2;
血沉方程k值可以在INSERT INTO中包含整个⽂件的bytea类型字符串,也可以像上⾯第四⾏那样,分块追加。对于短⼩的⼆进制数据,在命令控制台中编辑SQL命令也未尝不可。但是如果要存储⼀个图⽚⽂件或者Word⽂档之类的⼤型⼆进制数据的时候,就需要借助数据访问借⼝,或者⾃⼰写⼀个字节转换程序,直接操作SQL语句。
插⼊bytea数据后可以使⽤SELECT语句获取它。如下所⽰:
可兰经SELECT content FROM table1;
在命令控制台中,我们会看到以输⼊时的字符串格式输出⼆进制数据,这是PostgreSQL做的转换。在Python中使⽤psycopg2模块,执⾏上述SELECT语句后能够获得原始的⼆进制字节字符串,可以直接写⼊⼆进制⽂件。
顺便说明⼀下。对于字节的转换,PostgreSQL的⽂档说的⾮常详细,按照零字节、单引号、斜线,以及字符的可打印性分别作了讨论。原因是需要逃逸单引号和斜线字符,另外可打印字符可以不作转换,直接出现。
3)
PostgreSQL-Bytea存BlobDAta出错
⽤PostgreSQL-Bytea存BlobDAta,如mdb/mp3/jpg/doc等檔案,試了好幾天,是可以存進去,可是轉出來時⽼是無法使⽤,經研究發現它的體積會⾃動⾧⼤,且會以3.31的⽐率增加。這轉出來檔當然是不能⽤了。⽽且我⽤BlobField.BolbSize去看資料庫中的存檔⼤⼩就是這個轉出的Size,這表⽰是在存⼊時出了問題。這要如何處理?
設定可以處理Bytea-Blob存取BinaryData的功能
1.編修f:程式集->PostgreSQL8.2->編修控制檔->f。
2.改Connections and Authentication-section,加 listen_addresses = '*'
3.改Version/platform Compatibility加兩⾏
escape_string_warning = off
standard_conforming_strings = off
別⼩看這三⾏,⼩弟這三⾏可是花了三四個星期,把⼿冊全印出來裝釘研讀多次,還去PG姥姥家及Google翻江倒海了好久,最後還是靠Multics前輩指點才過關的。
真是眾裏尋它千百度,⾐帶漸寬終不悔,再回已百年⾝。烏乎哀哉
4)
1. ⽐如有⼀个实体定义例如:
/**
数据
*/
@Column(name = "f_data")
private byte[] data;
/**
数据
*/
@Column(name = "f_data2")
private Integer[] data2;
要保存到postgres过程中会出现data读取数据混乱,不是原来的数据,data2会出现⽆法反序列化问题
解决办法:修改postgres的f配置⽂件
bytea_output = 'escape'  # hex, escape
意思是设置bytea_output的输出类型设置为转义类型输出,⽽postgres默认是hex类型输出,所以导致转换数据混乱问题
参考⽂档:
8.4. Binary Data Types
The bytea data type allows storage of binary strings; see Table 8-6.
...
18.10. Client Connection Defaults
18.10.1. Statement Behavior
bytea_output (enum)
Sets the output format for values of type bytea. Valid values are hex (the default) and escape (the traditional PostgreSQL format). See Section 8.4 for more information. The bytea type always accepts
both formats on input, regardless of this setting.
以上配置需要重启postgres服务才⽣效
5)
参考资料
PostgreSQL Document:
PostgreSQL JDBC Interface:
⼆进制类型bytea的操作(在最⼤值内,有内存限制)
1Create table byteatable(id int,obj bytea);
①直接插⼊逃逸序列
bytea ⽂本逃逸⼋进制
一周立波秀2011集全集高清
⼗进制数值描述输⼊逃逸形式例⼦输出形式图像处理与模式识别
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
1Insert into byteatable values(1, '''');  //插⼊⼀个单引号-‘
②  通过base64的encode编码字符串
你好  编码为  5L2g5aW9
1Insert into byteatable values(1,decode(‘5L2g5aW9’,’base64’));//5L2g5aW9是【你好】编码后的代码
③  通过pg_read_binary_file()函数
1Insert 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
⼤对象类型oid的操作(在最⼤值内,没有内存限制)
1Create table oidtable(id int, obj oid);
2Insert into oidtable(id,obj) values(1,lo_import(‘d:/1.jpg’));
3select 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 字)。
西方文化Bytea与oid的⽐较
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
6)
asked this question · 0 karma ·
Is there a way to use the easy binary installer to install the bundled tomcat / jira install but use mysql or postegres instead of hsqldb?
373 views
One Answer:
· 2,885 karma ·
Hi Zippy,
After you install confluence with the binary installer, you can hit confluence in the browser, put the license over there, and choose between evaluation or production. As you choose production, you can select the database which you would
like to run confluence with.摆线
More information can be found  and .
I hope this helps!
Rodrigo
给我⽼师的⼈⼯智能教程打call!

本文发布于:2024-09-23 11:24:09,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/683647.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   类型   进制   输出   需要   字节   字符串   命令
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议