我有一个 PostgreSQL 9.1 数据库,其中图片存储为大对象。有没有办法通过 SQL 查询将文件导出到客户端文件系统?
select lo_export(data,'c:\img\test.jpg') from images where id=0;
我正在寻找一种类似于上述行的方法,但以客户端为目标。提前致谢!
我有一个 PostgreSQL 9.1 数据库,其中图片存储为大对象。有没有办法通过 SQL 查询将文件导出到客户端文件系统?
select lo_export(data,'c:\img\test.jpg') from images where id=0;
我正在寻找一种类似于上述行的方法,但以客户端为目标。提前致谢!
这个答案很晚,但会有所帮助,所以我敢肯定
要将图像从服务器获取到客户端系统,您可以使用它
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h 192.168.1.101 -p 5432 -d mDB -U mYadmin -c "\lo_export 19135 'C://leeImage.jpeg' ";
在哪里
文档在这里commandprompt.com
乔治,
根据9.1 的文档, lo_export 与执行调用的客户端相关。因此,如果 clientA 连接到 databaseB,当 clientA 执行您的 SQL 时,lo_export 应该在您告诉它的地方创建 clientA 上的文件。
鉴于您已经声明您在 MATLAB 下使用 JDBC(我不熟悉您可以在那里执行的操作,也不熟悉执行调用的接口),如果您从 JDBC 连接调用它手动:
java.sql.Connection conn= ...
java.sql.Statement stmt= conn.createStmt();
java.sql.ResultSet rs= stmt.executeQuery("select data from images where id=0");
// Assume one result
rs.next();
// Gets the blob input stream
InputStream blobData= rs.getInputStream(1);
// At this point you will have to write it to a file.
// See below
rs.close();
stmt.close();
conn.close();
为简洁起见,我对 JDBC 操作的使用非常松散和快速。应该有更多的错误检查以及 try/catch/finally 语句来包装和清理连接。
这是不可能的,因为 PostgreSQL 服务器所能做的就是通过客户端建立的网络连接将数据发送回客户端。特别是,它不能在客户端文件系统上创建文件,只有客户端代码可以这样做。
来源:http ://www.postgresql.org/docs/8.4/static/lo-funcs.html
CREATE TABLE image (
name text,
raster oid
);
SELECT lo_creat(-1); -- returns OID of new, empty large object
SELECT lo_create(43213); -- attempts to create large object with OID 43213
SELECT lo_unlink(173454); -- deletes large object with OID 173454
INSERT INTO image (name, raster)
VALUES ('beautiful image', lo_import('/etc/motd'));
INSERT INTO image (name, raster) -- same as above, but specify OID to use
VALUES ('beautiful image', lo_import('/etc/motd', 68583));
SELECT lo_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';