2017-04-01 92 views
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") 
+0

psql.drop(x,y)的預期結果是什麼,當你運行它時會得到什麼結果? – MrFlick

回答

0

你的功能psql.exists.boolean作爲輸入第一個表名,然後是架構名稱。 但是,當你在psql.drop中調用它時,首先給出它的模式名稱,然後是表名(psql.exists.boolean(y,x))。

+0

謝謝,這工作! –