Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active June 10, 2021 08:47
Show Gist options
  • Select an option

  • Save simonthompson99/0d41eb1e8a838b1309bd1c2a5dbcf6c4 to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/0d41eb1e8a838b1309bd1c2a5dbcf6c4 to your computer and use it in GitHub Desktop.
[Run query from file] Run query from sql file on specific connection #r #sql #database
run_query <- function(conn, f) {
# run the contents of sql file - f- on a given db connection - conn
require(DBI)
q <- trimws(readLines(f))
# remove lines starting with --
q <- gsub("^--.*$", "", q)
d <- dbGetQuery(conn, paste(q, collapse = " "))
cat(paste0(f, ' - ', nrow(d), 'Rx', ncol(d), 'C\n'))
return(d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment