简体   繁体   中英

create new table in MS Access database with R

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?

You're right to check if the file exists first since, as far as I can tell, there is no way for R to create a new Access database. Base R's file.create() function acts like it can, but when you try to open it up in Access a popup will arise saying it's not a valid database (accordingly, R won't be able to write to it either).

My workaround for this is to create a blank .accdb file somewhere on my computer and then use R's built-in file management functions to copy it, rename it, and move it to my working directory whenever I want to export to Access:

blank_db <- "C:/Users/JohnDoe/Desktop/foo.accdb" #a valid but empty Access database, previously created outside of R
db_file_path <- "Z:/American Kestrel projects/Idaho banding data/American Kestrels_2018_May15.accdb"

if (!file.exists(db_file_path)) file.copy(blank_db, db_file_path) #copies, renames, and moves empty Access data base to target path

You can then create new tables in that copied blank Microsoft Access database using the sqlSave() function in the RODBC package:

data(mtcars)
cars1 <- mtcars
cars2 <- mtcars

library(RODBC)
db <- odbcConnectAccess2007(db_file_path)
sqlSave(db, cars1, tablename = "MTCARS 1", fast = TRUE, safer = FALSE, rownames = FALSE, colnames = FALSE)
sqlSave(db, cars2, tablename = "MTCARS 2", fast = TRUE, safer = FALSE, rownames = FALSE, colnames = FALSE)
odbcClose(db)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM