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

时间:2016-07-08 18:32:53

标签: rodbc

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

查询:INSERT INTO" Trip" (" CruiseID"," VessName"," LOA"," HrsePowr"," DocNum"," PermNum"," Captain"," GearWid"," CrewSize"," Grounds"," DateBeg", " DateEnd"," ProjDesc","评论")价值观(?,?,?,?,?,?,?,?,?,?,? ,?,?,?) odbcUpdate出错(channel,query,mydata,coldata [m,],test = test,:   缺少'数据'

中的列
  

Dataframe我正在尝试导出

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

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

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

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 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

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

R信息

ListBox

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

1 个答案:

答案 0 :(得分:0)

注意这里有什么不对吗?

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个占位符......