【发布时间】:2012-01-09 10:36:00
【问题描述】:
我有一个 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
【讨论】:
乔治,
根据documentation for 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服务器所能做的就是通过客户端建立的网络连接将数据发送回客户端。 特别是,它不能在客户端文件系统上创建文件,只有客户端代码可以这样做。
【讨论】:
\lo_export lo_oid filename The large object export command; this lets you export the large object with OID lo_oid to filename on your local filesystem. This is different from the lo_export() server function in the same way the \copy and the SQL COPY commands are different. 可能来自commandprompt.com/ppbook/c12119
来源: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';
【讨论】: