简体   繁体   English

使用 R 在 MS Access 数据库中创建新表

[英]create new table in MS Access database with R

I'd like to create a new table in a MS Access database.我想在 MS Access 数据库中创建一个新表。 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:我只是在做一个虚拟示例,我现在将“mtcars”数据集添加到其中,抱歉,这不是真正可重现的,但也许有一个简单的解决方案:

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. new_result(connection@ptr, statement) 错误:nanodbc/nanodbc.cpp:1344: 42000: [Microsoft][ODBC Microsoft Access Driver] CREATE TABLE 语句中的语法错误。

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.您首先检查文件是否存在是正确的,因为据我所知,R 无法创建新的 Access 数据库。 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). Base R 的file.create()函数就像它可以的那样,但是当您尝试在 Access 中打开它时,会出现一个弹出窗口,说它不是一个有效的数据库(因此,R 也无法写入它)。

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:我的解决方法是在我的计算机上的某个地方创建一个空白的 .accdb 文件,然后使用 R 的内置文件管理功能来复制它,重命名它,并在我想导出到 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:然后,您可以使用RODBC包中的sqlSave()函数在复制的空白 Microsoft Access 数据库中创建新表:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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