簡體   English   中英

從R向Access ODBC數據庫中的現有表添加新列

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

我試圖在使用R訪問的現有表中添加新列。

我不知道該怎么做。 這是我嘗試的:

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

我也嘗試使用sqlSave命令,但據我所知,它只是讓您追加新行。 是數據庫結構問題還是我用R命令做錯了什么?

提前致謝。

看起來sqlUpdate()不會自動更改表的結構(不是我期望的那樣)。 所以,您可能必須要像

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

在嘗試應用更新之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM