用户定义函数返回意外结果

时间:2017-04-01 02:22:02

标签: r database postgresql postgresql-9.6

这是我在stackoverflow.com上的第一个问题!

我创建了以下函数来检查PostgreSQL数据库中是否存在和删除表,包括删除前后的表。不幸的是,删除功能并没有给我预期的输出。当我使用输入评估psql.exists.boolean(x,y)时,它返回预期的结果TRUE,但是当我使用相同的输入评估psql.drop(x,y)时,它不会返回预期的结果。请给我一些指导,以解决我的错误功能。

psql.drop<-function(x,y){
table.bad<-x
dbschema<-y
db.location <- c(y,x)
if (psql.exists.boolean(y,x)==TRUE){
  dbRemoveTable(con,db.location)
    if (psql.exists.boolean(y,x)==TRUE){
      msg<-paste("ERROR! The table",dQuote(paste(db.location, collapse = '.')),"still exists!")
  } else if (psql.exists.boolean(y,x)==FALSE){
      msg<-paste("The table",dQuote(paste(db.location, collapse = '.')),"was successfully removed.")
  } else {
     msg<-paste("ERROR! Something went wrong.")
  }
} else {
  msg<-paste("The table",dQuote(paste(db.location, collapse = '.')),"doesn't exist.")
}
return(print(msg))

psql.drop("test_table_1","test_schema_1")

psql.exists.boolean<-function(x,y){
  # table name to check
  table.name<-x
  # schema where table is stored
  dbschema<-y
  # name format in schema  
  db.location <- c(dbschema, table.name)
  # check if table existence in specified location is true
  if(dbExistsTable(con,db.location)==TRUE){
    return(TRUE)
  }else{
    return(FALSE)
  }
}

psql.exists.boolean("test_table_1","test_schema_1")

1 个答案:

答案 0 :(得分:0)

您的函数psql.exists.boolean首先将表名称作为输入,然后是模式名称。 但是当你在psql.drop中调用它时,首先给它模式名称,然后是表名(psql.exists.boolean(y,x))。

相关问题