0

我安装了 PL/R (plr) 并用它扩展了我的数据库。

我创建了一个从绘图创建 pdf 打印的函数。数据是从我的 PostgreSQL 数据库中查询的:

CREATE OR REPLACE FUNCTION f_graph() RETURNS text AS
'
require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="pass",dbname="landslide", port="5432")
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=1")
section1 <- fetch(rs, 2000)
pdf("/tmp/myplot.pdf", width=18, height=12)
plot(section1$y, section1$z, xlab="distance in m", ylab="altitude in m a.s.l.",main="Section Einbühl Ebermannstadt", type="l", lwd=1.5, lty=3)
dev.off()
print("done")
'
LANGUAGE 'plr' VOLATILE STRICT;

但是当我想在这个扩展函数中查询两个结果集(section1、section2)时:

CREATE OR REPLACE FUNCTION f_graph() RETURNS text AS
'
require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="pass", dbname="landslide", port="5432")
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=1")
section1 <- fetch(rs, 2000)
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=2")
section2 <- fetch(rs, 2000)
pdf("/tmp/myplot.pdf", width=18, height=12)
plot(section1$y, section1$z, xlab="distance in m", ylab="altitude in m a.s.l.", main="Section Einbühl Ebermannstadt", type="l", lwd=1.5, lty=3)
lines(section2$y, section2$z, xlab="", ylab="", type="l", lwd=2.5, col="red")
dev.off()
print("done")
'
LANGUAGE 'plr' VOLATILE STRICT;

出现以下错误:

landslide=# SELECT f_graph();
ERROR:  R interpreter expression evaluation error
DETAIL:  Error in pg.spi.cursor_open("plr_cursor", plan) : 
error in SQL statement : cursor "plr_cursor" already exists
CONTEXT:  In R support function pg.spi.cursor_open
In PL/R function f_graph

如何解决这个问题?我可以设置多个 plr_cursors 吗?

4

1 回答 1

1

我认为您需要在询问更多数据之前清除结果。

dbClearResult得到第一个结果后尝试使用

section1 <- fetch(rs, 2000)
dbClearResult(rs)

即使没有产生错误,dbClearResult(rs)在最后一次获取之后使用,在关闭连接之前使用dbDisconnect(con).

于 2013-11-29T14:42:20.163 回答