myPostgresDropTable.R 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #' PostgreSQL - Delete postgresql database table
  2. #' @details PostgreSql andmebaasi tabeli kustutamine.
  3. #' @param con An PostgreSQLConnection object as produced by dbConnect.
  4. #' @param tbl The database table name.
  5. #' @return eturn message.
  6. #' @seealso \code{\link{myPostgresConnect}}, \code{\link{myPostgresDropTable}},
  7. #' \code{\link{myPostgresCreateGisDb}}, \code{\link{myPostgresImprotShp}}
  8. #' @examples \dontrun{
  9. #' con <- myPostgresConnect(host = '192.168.255.26', port = 5432,
  10. #' user = 'osm', password = 'osm', dbname = 'shp')
  11. #' myPostgresDropTable(con = con, tbl = "xxx")
  12. #'
  13. #' }
  14. #'
  15. #' @export
  16. #'
  17. myPostgresDropTable <- function(con = NULL, tbl) {
  18. # Check is connection valid
  19. if(is.null(con) || !DBI::dbIsValid(con)){
  20. message(paste0("DB connection is not valid!"))
  21. return()
  22. }
  23. # Check if db exists
  24. if(!DBI::dbExistsTable(con, tolower(tbl))){
  25. DBI::dbDisconnect(con)
  26. message(paste0("Tabel ", tbl, " doesn't exist."))
  27. return()
  28. }
  29. # Drop table
  30. q <- paste("drop table if exists ", tbl, ";")
  31. res <- DBI::dbSendQuery(con, q)
  32. DBI::dbHasCompleted(res)
  33. DBI::dbClearResult(res)
  34. DBI::dbDisconnect(con)
  35. }