PostgeSQL \lo_import和如何获得生成的OID到UPDATE命令? [英] PostgeSQL \lo_import and how to get the resulting OID into an UPDATE command?

查看:280
本文介绍了PostgeSQL \lo_import和如何获得生成的OID到UPDATE命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Postgres 9.0,我有一个应用程序,我需要插入图像到远程服务器。所以我使用:

 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从以前的命令..如何获取OID?
WHERE species ='ACCOAA';

所以我的问题是:如何获得 OID
$ b

我试过运行 \\ <$ c> \\ r \\ n \\ c \\ c>在Postgres,但我得到一个错误:

  ERROR:\lo_import'或其附近的语法错误C://im/zzz4.jpg'
LINE 1:\lo_import'C://im/zzz4.jpg'

尝试过:

 更新种类
set speciesimages = \lo_import'C://im/zzz4.jpg'
其中species ='ACAAC04';

但我收到此错误:

 错误:\或其附近的语法错误
LINE 2:set speciesimages = \lo_import'C://im/zzz4.jpg'
^


解决方案

由于您的文件驻留在本地计算机上,将blob导入到远程服务器,您有两个选项:



1)将文件传输到服务器并使用服务器端功能

  UPDATE species 
SET speciesimages = lo_import('/ path / to / server-local / file / zzz4.jpg')
WHERE species ='ACAAC04';

2)使用 psql元命令



但是你不能将psql元命令与SQL命令混合,这是不可能的。

:LASTOID
> $ UPDATE
命令,在同一个psql会话中 \lo_import 元命令后立即启动:

  UPDATE species 
SET speciesimages =:LASTOID
WHERE species ='ACAAC04';

对于脚本(在Linux中工作,我不熟悉Windows shell脚本):

  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




  • \\ 是separator元命令。您需要将 \ 加倍到字符串中,因为shell解释一个图层。

  • \ 之前换行符只是Linux shell中的行继续。



替代语法(在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


I'm working with Postgres 9.0, and I have an application where I need to insert images into the remote server. So I use:

 "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'";

where

192.168.1.12 is the IP address of the server system

5432 is the Port number

myDB is server database name

my_admin is the username

"\lo_import 'C://im/zzz4.jpg'" is the query that is fired.

After the image has been inserted into the database I need to update a row in a table like this:

UPDATE species 
SET    speciesimages=17755;  -- OID from previous command.. how to get the OID ??
WHERE  species='ACCOAA';

So my question is: how do I get the OID returned after the \lo_import in psql?

I tried running \lo_import 'C://im/zzz4.jpg' in Postgres but I get an error:

ERROR:  syntax error at or near ""\lo_import 'C://im/zzz4.jpg'""
LINE 1: "\lo_import 'C://im/zzz4.jpg'"

I also tried this:

update species
set speciesimages=\lo_import 'C://im/zzz4.jpg'
where species='ACAAC04';

But I get this error:

ERROR:  syntax error at or near "\"
LINE 2: set speciesimages=\lo_import 'C://im/zzz4.jpg'
                          ^

解决方案

As your file resides on your local machine and you want to import the blob to a remote server, you have two options:

1) Transfer the file to the server and use the server-side function:

UPDATE species
SET    speciesimages = lo_import('/path/to/server-local/file/zzz4.jpg')
WHERE  species = 'ACAAC04';

2) Use the psql meta-command like you have it.

But you cannot mix psql meta commands with SQL-commands, that's impossible.
Use the psql variable :LASTOID in an UPDATE command that you launch immediately after the \lo_import meta command in the same psql session:

UPDATE species
SET    speciesimages = :LASTOID
WHERE  species = 'ACAAC04';

To script that (works in Linux, I am not familiar with Windows shell scripting):

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

  • \\ is the separator meta-command. You need to double the \, in a "" string, because the shell interprets one layer.
  • \ before the newline is just the line continuation in Linux shells.

Alternative syntax (tested on Linux again):

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

这篇关于PostgeSQL \lo_import和如何获得生成的OID到UPDATE命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆