简体   繁体   中英

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.

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. Is it a database structure problem or am I doing something wrong with the R commands?

Thanks in advance.

It looks like sqlUpdate() does not alter the structure of the table automatically (not that I would expect it to). 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.

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