Advanced Database Management with R
Instruction: Describe how to connect to and perform operations on a SQL database using R.
Context: This question is designed to test the candidate's ability to interface R with SQL databases, including data extraction, manipulation, and storage.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Use DBI with a supported database driver, bind values as parameters, and close connections/results reliably. This complete example uses only an in-memory SQLite database.
run_example <- function() { con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") on.exit(DBI::dbDisconnect(con), add = TRUE) DBI::dbExecute(con, "CREATE TABLE sales(id INTEGER PRIMARY KEY, amount REAL)") DBI::dbWithTransaction(con, { DBI::dbExecute(con, "INSERT INTO sales(id, amount) VALUES (?, ?)", params = list(1L, 10)) DBI::dbExecute(con,...