将 sqlSave 与 Microsoft Access 数据库一起使用

Posted

技术标签:

【中文标题】将 sqlSave 与 Microsoft Access 数据库一起使用【英文标题】:use sqlSave with Microsoft Access database 【发布时间】:2016-07-08 18:32:53 【问题描述】:

我正在学习如何使用 RODBC 包将数据帧导出到 Microsift Access 数据库中。数据库已经创建了我正在尝试将数据框导出到的表。我尝试导出的第一个简单数据框给出以下错误消息:

查询:插入“Trip”(“CruiseID”、“VessName”、“LOA”、“HrsePowr”、“DocNum”、“PermNum”、“Captain”、“GearWid”、“CrewSize”、“Grounds” , "DateBeg", "DateEnd", "ProjDesc", "Comments" ) 值 (?,?,?,?,?,?,?,?,?,?,?,?,?,?) odbcUpdate(channel, query, mydata, coldata[m, ], test = test, 中的错误: “数据”中缺少列

我正在尝试导出的数据框

trip_3<-dput(trip_3)
structure(list(CruiseID = 201602:201605, `Vessel Name` = structure(c(3L, 
4L, 2L, 1L), .Label = c("KATE", "F/V Celtic", "F/V Carolina Capes II", 
"Sea Hawk"), class = "factor"), LOA = c(NA, NA, 100L, NA), HrsePowr = c(NA, 
NA, 1150L, NA), DocNum = c(NA, NA, 591971L, 1159450L), PermNum = c(NA, 
NA, 410146L, 410561L), Captain = structure(c(NA, NA, 2L, 1L), .Label = c("William Hullbig", 
"Charlie Quinn"), class = "factor"), GearWid = c(14L, NA, 8L, 
15L), CrewSize = c(6L, 6L, 6L, 13L), Grounds = structure(c(NA, 
3L, 2L, 1L), .Label = c("CAII ", "NLCA", "MAB"), class = "factor"), 
    DateBeg = structure(c(1462248000, 1463544000, 1464926400, 
    1466481600), class = c("POSIXct", "POSIXt"), tzone = ""), 
    DateEnd = structure(c(NA, NA, NA, 1467172800), class = c("POSIXct", 
    "POSIXt"), tzone = ""), ProjDesc = structure(c(3L, 4L, 2L, 
    1L), .Label = c("CAII Survey", "2016 RSA NLCA Survey ", "MAB Survey Leg 1", 
    "2016 RSA MAB Survey Leg 2"), class = "factor"), Comments = structure(c(3L, 
    4L, 2L, 1L), .Label = c("survey dredge 9 on port and NB dredge on starboard", 
    "Survey dredge on Port #8, 14 ft NBD on Stbd.", "MAB Survey Leg 1, dredge 8 on starboard side", 
    "survey dredge on starboard side, no 9"), class = "factor")), .Names = c("CruiseID", 
"Vessel Name", "LOA", "HrsePowr", "DocNum", "PermNum", "Captain", 
"GearWid", "CrewSize", "Grounds", "DateBeg", "DateEnd", "ProjDesc", 
"Comments"), class = "data.frame", row.names = c(3L, 4L, 2L, 
1L))

如果我确实将数据框导出到新表,它可以工作,尽管字段 VessName 已更改。我最初将其作为 Vessel Name,但导出将其转换为 VesseName,因此我只是更改了数据库中的名称。我想在数据库中有正确的名称。

为了尝试处理错误消息,我已将 odbc 连接设置为

db2016<-odbcDriverConnect("db2016",case="nochange")

我也一直在阅读其他端口并尝试了以下内容:

tmp <- sqlColumns(db2016, "Trip")
varTypes = as.character(tmp$TYPE_NAME)

[1] "VARCHAR"  "VARCHAR"  "SMALLINT" "SMALLINT" "INTEGER" 
 [6] "INTEGER"  "VARCHAR"  "SMALLINT" "SMALLINT" "VARCHAR" 
[11] "DATETIME" "DATETIME" "VARCHAR"  "VARCHAR"


names(varTypes) = as.character(tmp$COLUMN_NAME)

CruiseID   VessName        LOA   HrsePowr     DocNum 
 "VARCHAR"  "VARCHAR" "SMALLINT" "SMALLINT"  "INTEGER" 
   PermNum    Captain    GearWid   CrewSize    Grounds 
 "INTEGER"  "VARCHAR" "SMALLINT" "SMALLINT"  "VARCHAR" 
   DateBeg    DateEnd   ProjDesc   Comments 
"DATETIME" "DATETIME"  "VARCHAR"  "VARCHAR" 

colspec<-list(tmp$TYPE_NAME)

[[1]]
 [1] "VARCHAR"  "VARCHAR"  "SMALLINT" "SMALLINT" "INTEGER" 
 [6] "INTEGER"  "VARCHAR"  "SMALLINT" "SMALLINT" "VARCHAR" 
[11] "DATETIME" "DATETIME" "VARCHAR"  "VARCHAR" 

我可以导出数据的唯一方法是将数据转到新表。我还有其他几个要导出的数据框,并且数据库的关系已经定义,所以我不想创建新表。

R 信息

R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] datasets  utils     stats     graphics  grDevices methods  
[7] base     

other attached packages:
 [1] xlsx_0.5.7          xlsxjars_0.6.1      rJava_0.9-8        
 [4] stringr_1.0.0       rgdal_1.1-10        plyr_1.8.4         
 [7] data.table_1.9.6    gmt_1.2-0           lubridate_1.5.6    
[10] maptools_0.8-39     sp_1.2-3            MASS_7.3-45        
[13] RODBC_1.3-13        latticeExtra_0.6-28 RColorBrewer_1.1-2 
[16] lattice_0.20-33    

loaded via a namespace (and not attached):
[1] Rcpp_0.12.5    chron_2.3-47   grid_3.3.1     magrittr_1.5  
[5] stringi_1.1.1  tools_3.3.1    foreign_0.8-66

Microsoft Access 信息:Microsoft Office Professional Plus 2010 32 位。

【问题讨论】:

【参考方案1】:

注意到这里有什么问题吗?

INSERT INTO "Trip" (

   "CruiseID", "VessName", "LOA", "HrsePowr", "DocNum", "PermNum",
       1           2          3        4           5         6
   "Captain", "GearWid", "CrewSize", "Grounds", "DateBeg", "DateEnd",
       7           8          9          10         11        12
   "ProjDesc"
       13
) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,? )
           1 2 3 4 5 6 7 8 9 1 1 1
                             0 1 2

13 个字段,12 个占位符...

【讨论】:

我仔细检查了我的代码并重新运行了所有内容。我收到类似的错误消息,但这次字段和占位符的数量匹配。

以上是关于将 sqlSave 与 Microsoft Access 数据库一起使用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 R 中使用 SQLsave 进行查询?

sqlSave 出错

RODBC 与 ms-access 错误 07002 17 [Microsoft][ODBC Microsoft Access Driver]COUNT 字段不正确

RODBC sqlSave() 和映射列名

RODBC sqlSave 崩溃 R 前端

如何使用 R 中的函数 sqlSave() 将数据附加到具有 IDENTITY 主键的 SQL Server 表中?