I'd like to create a new table in a MS Access database. I'm just working on a dummy example where I add the 'mtcars' dataset to it for now, sorry this isn't really reproducible but maybe there's an easy solution out there:
connect_to_access_dbi <- function(db_file_path) {
require(DBI)
# make sure that the file exists before attempting to connect
if (!file.exists(db_file_path)) {
stop("DB file does not exist at ", db_file_path)
}
# Assemble connection strings
dbq_string <- paste0("DBQ=", db_file_path)
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
db_connect_string <- paste0(driver_string, dbq_string)
myconn <- dbConnect(odbc::odbc(),
.connection_string = db_connect_string)
return(myconn)
}
# get to Access database(s) through research drive path
db_file_path <- 'Z:/American Kestrel projects/Idaho banding data/American Kestrels_2018_May15.accdb'
# connect to database
con <- connect_to_access_dbi(db_file_path)
data(mtcars)
# add new mtcars table
dbWriteTable(con, "mtcars", mtcars)
dbReadTable(con, "mtcars")
This doesn't work, and I get an error message like this:
Error in new_result(connection@ptr, statement) : nanodbc/nanodbc.cpp:1344: 42000: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
Would anyone know what that means and how I can make this work?