我收到这个INSERT INTO的错误,我似乎无法弄明白。代码遍历Dao记录集,仅将某些记录附加到表中。
enter code here
preprocess <- function(x){
##flipping data to suit time series analysis
my.data <- x[nrow(x):1,]
#sort(x,)
## setting up date as date format
my.data$date <- as.Date(my.data$date)
##creating a new data frame to sort the data.
sorted.data <- my.data[order(my.data$date),]
#removing the last row as it contains stocks price at moment when i downloaded data
#sorted.data <- sorted.data[-nrow(sorted.data),]
#calculating lenght of the data frame
data.length <- length(sorted.data$date)
## extracting the first date
time.min <- sorted.data$date[1]
##extracting the last date
time.max <- sorted.data$date[data.length]
# creating a new data frame with all the dates in sequence
all.dates <- seq(time.min, time.max, by="day")
all.dates.frame <- data.frame(list(date=all.dates))
#Merging all dates data frame and sorted data frame; all the empty cells are assigned NA vales
merged.data <- merge(all.dates.frame, sorted.data, all=T)
##Replacing all NA values with the values of the rows of previous day
final.data <- transform(merged.data, close = na.locf(close), open = na.locf(open), volume = na.locf(volume), high = na.locf(high), low =na.locf(low))
# write.csv(final.data, file = "C:/Users/rites/Downloads/stock prices", row.names = FALSE)
#
#return(final.data) #--> ##{remove comment for Code Check}
################################################################
######calculation of CAGR(Compound Annual Growth Rate ) #######
#### {((latest price/Oldest price)^1/#ofyears) - 1}*100 ########
################################################################
##Extracting closing price of the oldest date
old_closing_price <- final.data$close[1]
##extracting the closing price of the latest date
new_closing_price <- final.data$close[length(final.data$close)]
##extracting the starting year
start_date <- final.data$date[1]
start_year <- as.numeric(format(start_date, "%Y"))
##extracting the latest date
end_date <- final.data$date[length(final.data$date)]
end_year <- as.numeric(format(end_date, "%Y"))
CAGR_1 <- new_closing_price/old_closing_price
root <- 1/(end_year-start_year)
CAGR <- (((CAGR_1)^(root))-1)*100
return (CAGR)
}
temp = list.files(pattern="*.csv")
for (i in 1:length(temp))
assign(temp[i], preprocess (read.csv(temp[i])))
我在INSERT INTO语句中一直收到语法错误,但我已经检查了几次。
编辑 - 解释。标签
答案 0 :(得分:1)
带有空格或标点符号/特殊字符的名称(只有例外是下划线)需要包含在[]中。最好在命名约定中避免这些。但是,对带有引号的记录集字段的引用应该无需包含在[]中。需要[]的替代语法是:rs![Eff Date]
。
sqlinsert = "INSERT INTO 400_CF_BREAK_LOG ([Eff Date], [PrimarySecurity ID Number],
[CUSIP(Aladdin ID)], IsrName, [Asset Type], [Metlife Port Code], [Business Unit],
[Principal Difference], [Total PAM Principal], [Total Aladdin Principal],
[Income Difference], [Total PAM Interest], [Total Aladdin Interest], [Total CF Difference],
[Total PAM CF], [PAM Coupon])" & _
" VALUES ('" & rs("Eff Date") & "', '" & rs("PrimarySecurity ID Number") & "', '" &
rs("CUSIP(Aladdin ID)") & "', '" & rs("IsrName") & "', '" & rs("Asset Type") & "', '" &
rs("Metlife Port Code") & "', '" & rs("Business Unit") & "', '" & rs("Principal Difference") & "', '" &
rs("Total PAM Principal") & "', '" & rs("Total Aladdin Principal") & "','" &
rs("Income Difference") & "', '" & rs("Total PAM Interest") & "', '" &
rs("Total Aladdin Interest") & "', '" & rs("Total CF Difference") & "', '" & rs("Total PAM CF") & "', '" &
rs("PAM Coupon") & "') "