简体   繁体   English

从R向Access ODBC数据库中的现有表添加新列

[英]Adding a new column to an existing table in an access ODBC database from R

I am trying to add a new column to an existing table in access using R. 我试图在使用R访问的现有表中添加新列。

I cannot figure out the way to do it. 我不知道该怎么做。 This is what I tried: 这是我尝试的:

install.packages("RODBC")
require(RODBC)

channel <- odbcConnect("Access01", believeNRows=FALSE)
df <- sqlFetch(channel, "Customers")
df

 ID Last_Name First_Name  Email_Address Business_Phone Home_Phone Mobile_Phone Fax_Number        Address
1  1       Sam      Marty  mlast@123.com     7771234567 8882626262   9998283838 5551717171   123 Main St.
2  2       Sam      Sally sfirst@123.com     5557778888 5558889999   5559991111 5552223333 234 Second Ave
      City State_Province ZIP_Postal_Code Country_Region Sex Date_of_birth
1 Anywhere             ST           55555            USA   M    1960-02-03
2   guirao             ST           22222            USA   f    1975-12-12


df <- cbind(df, test=c("A", "B")
df

 ID Last_Name First_Name  Email_Address Business_Phone Home_Phone Mobile_Phone Fax_Number        Address
1  1       Sam      Marty  mlast@123.com     7771234567 8882626262   9998283838 5551717171   123 Main St.
2  2       Sam      Sally sfirst@123.com     5557778888 5558889999   5559991111 5552223333 234 Second Ave
      City State_Province ZIP_Postal_Code Country_Region Sex Date_of_birth test
1 Anywhere             ST           55555            USA   M    1960-02-03    A
2   guirao             ST           22222            USA   f    1975-12-12    B


sqlUpdate(channel, df, tablename = "Customers", index="ID")

**Error in sqlUpdate(channel, df, tablename = "Customers", index = "ID") : 
  data frame column(s) test not in database table**

I also tried to use the sqlSave command but as far as I know it just let you append new rows. 我也尝试使用sqlSave命令,但据我所知,它只是让您追加新行。 Is it a database structure problem or am I doing something wrong with the R commands? 是数据库结构问题还是我用R命令做错了什么?

Thanks in advance. 提前致谢。

It looks like sqlUpdate() does not alter the structure of the table automatically (not that I would expect it to). 看起来sqlUpdate()不会自动更改表的结构(不是我期望的那样)。 So, you'll probably have to so something like 所以,您可能必须要像

sqlQuery(channel, 'ALTER TABLE Customers ADD COLUMN test TEXT(1)')

before you try to apply the update. 在尝试应用更新之前。

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

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