2016-07-22 19 views
-1

我是R新手,嘗試在R Postgresql中插入R數據幀。每次每當我嘗試執行我的rscripts.R,我收到以下錯誤:R語言無法在postgresql中使用postgresqlWriteTable插入

「在postgresqlWriteTable(康涅狄格州,名稱,價值,...):中止assignTable:表customervalidation在數據庫中存在」

表customervalidation已經存在於postgresql中,我試圖在這個表中插入SampleData.csv的內容。 csv的所有標題都已經存在於表格中,並且全部都是小寫字母。

命令行參數

./script.R batch SampleData.csv yes no 

rscripts.R內容

#!/usr/bin/Rscript 

options(echo=TRUE) # if you want see commands in output file 
args <- commandArgs(trailingOnly = TRUE) 
print(args) 
# trailingOnly=TRUE means that only your arguments are returned, check: 
# print(commandsArgs(trailingOnly=FALSE)) 

batchIndicator <- tolower(args[1]) 
filename <- args[2] 
isHeaderPresent <-args[3] 
isRunTheBatch<-args[4] 
rm(args) 
#Library files 
library(RPostgreSQL) 
#now check whether it is immediate or batch. 
# if it is immediate then real time prediction needs to prepare. 
# if it is batch then whole batch set needs to prepare and keep the results in a separate file. 
if(isHeaderPresent == "yes") 
{ 
    header = TRUE 
}else 
{ 
    if(isHeaderPresent == "no"){ 

    header = FALSE 
    } 
} 

    print(paste("Processing for Batch mode for filename ", filename)) 
    # Start body for other function 
    data <-read.csv(filename,header = header, sep=",") 
    drv <- dbDriver("PostgreSQL") 
    con <- dbConnect(PostgreSQL(), dbname = "customervalidation", host = "localhost", port =5432 , user = "user", password = "pwd") 
    dbWriteTable(con,"customervalidation",data,row.names=FALSE) 
    #end body for other function 

而且SampleData.csv 的內容CSV content here

請幫我識別什麼是這裏缺少的錯誤。

回答

2

錯誤是不言自明的:該表已存在。如果你看一下manual你會看到有兩個選擇:

  • 覆蓋邏輯指定是否覆蓋現有表 與否。它的默認值是FALSE。
  • 附加邏輯,指定是否將 附加到DBMS中的現有表。它的默認值是FALSE。

你必須指定其中的一個爲TRUE。