I'm reading in a .csv file and comparing two fields, one in a table called "Col" and one in the .csv file called "newdata". If they match I overwrite item_price in the "COL" table with the value of avg_price in "newdata". I get no errors, but nothing is changed in COL.
library(RODBC)
db <- "C:/Projects/Online.accdb"
conn <- odbcConnectAccess2007(db)
newData <- read.csv("C:/Projects/duplicates.csv", stringsAsFactors = F)
for(row in 1:nrow(newData)){
query <- paste0(
"UPDATE COL
SET item_price = ",newData$avg_price[row],
"WHERE COL.generic ='",newData$generic[row],"'"
)
sqlQuery(conn, query)
}
close(conn)
From the comments, suggest a fix below, which will change the column names of the input file to match the names of the query:
library(RODBC)
db <- "C:/Projects/Online.accdb"
conn <- odbcConnectAccess2007(db)
newData <- read.csv("C:/Projects/duplicates.csv", stringsAsFactors = F)
names(newData) <- c("generic","avg_price")
for(row in 1:nrow(newData)){
query <- paste0("UPDATE COL SET item_price = ",newData$avg_price[row]," WHERE COL.generic ='",newData$generic[row],"'")
sqlQuery(conn, query)
}
close(conn)