0

这是我在 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")
4

1 回答 1

0

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

于 2017-04-01T20:51:02.777 回答