我正在使用Postgres9.0,我有一个应用程序,需要在远程server中插入图像。所以我用:
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h 192.168.1.12 -p 5432 -d myDB -U my_admin -c "\lo_import 'C://im/zzz4.jpg'";哪里
192.168.1.12是服务器系统的IP地址。
5432是端口号
myDB是服务器数据库名。
my_admin是用户名
"\lo_import 'C://im/zzz4.jpg'"是被触发的查询。

在将图像插入数据库后,我需要更新如下表中的一行:
UPDATE species
SET speciesimages=17755; -- OID from previous command.. how to get the OID ??
WHERE species='ACCOAA';因此,我的问题是:在psql中,如何在OID之后返回\lo_import?
我尝试在Postgres中运行\lo_import 'C://im/zzz4.jpg',但是我得到了一个错误:
ERROR: syntax error at or near ""\lo_import 'C://im/zzz4.jpg'""
LINE 1: "\lo_import 'C://im/zzz4.jpg'"I也尝试过这样做:
update species
set speciesimages=\lo_import 'C://im/zzz4.jpg'
where species='ACAAC04';但我知道这个错误:
ERROR: syntax error at or near "\"
LINE 2: set speciesimages=\lo_import 'C://im/zzz4.jpg'
^发布于 2012-03-20 12:13:25
由于您的文件驻留在本地计算机上,并且希望将blob导入到远程服务器,您有两个选项:
1)将文件传输到服务器并使用服务器端功能:
UPDATE species
SET speciesimages = lo_import('/path/to/server-local/file/zzz4.jpg')
WHERE species = 'ACAAC04';2)像使用psql元命令一样使用它。
但是你不能把psql元命令和SQL命令混在一起,这是不可能的。
在同一个psql会话中紧接在:LASTOID元命令之后启动的UPDATE命令中使用psql变量\lo_import:
UPDATE species
SET speciesimages = :LASTOID
WHERE species = 'ACAAC04';要编写该脚本(在Linux中工作,我不熟悉Windows脚本):
echo "\lo_import '/path/to/my/file/zzz4.jpg' \\\\ UPDATE species SET speciesimages = :LASTOID WHERE species = 'ACAAC04';" | \
psql -h 192.168.1.12 -p 5432 -d myDB -U my_admin\\是分隔符元命令.您需要在一个\字符串中加倍"",因为外壳只解释一个层。\只是Linux中的行延续。替代语法(再次在Linux上测试):
psql -h 192.168.1.12 -p 5432 -d myDB -U my_admin << EOF
\lo_import '/path/to/my/file/zzz4.jpg'
UPDATE species
SET speciesimages = :LASTOID
WHERE species = 'ACAAC04';
EOF发布于 2013-08-07 22:05:40
使用此命令导入图像后:
\lo_import '$imagePath' '$imageName'然后,您可以通过查询pg_catalog.pg_largeobject_metadata表来找到二进制文件的描述,该表存储所需的oid值。
Ie:
"SELECT oid as `"ID`",
pg_catalog.obj_description(oid, 'pg_largeobject') as `"Description`"
FROM pg_catalog.pg_largeobject_metadata WHERE pg_catalog.obj_description(oid,'pg_largeobject') = '$image' limit 1 "发布于 2019-12-20 06:47:51
如果您的字段是bytea类型,下面是如何做到这一点。
\lo_import '/cygdrive/c/Users/Chloe/Downloads/Contract.pdf'
update contracts set contract = lo_get(:LASTOID) where id = 77;导入后使用\lo_list和\lo_unlink。
https://stackoverflow.com/questions/9786014
复制相似问题