Hi,
I am looking for advices to create some helpfull functions in a corporate internal package to connect and query a MS SQLServer database and a MySQL server.
I have read many times the website https://db.rstudio.com/ but i'm unable to figure out a good way to start.
Should i dive into OO programming and create a class that have methods to connect and query the db ?
Should i just write functions like :
my_conn <- function(db, user, pass){
con <- DBI::dbConnect(odbc::odbc(),
Driver = "[driver's name]",
Server = "[server's path]",
Database = db,
UID = user,
PWD = pass,
Port = 1433)
con
}
How should i handle frequently used queries ? I have thought of functions like
my_super_query <- function(con, param1){
airport <- dbSendQuery(con, "SELECT * FROM airports WHERE faa = ?")
dbBind(airport, list(param1))
dbFetch(airport)
}
and documenting the query in the function documentation, but is there better ways to do it ?