简体   繁体   English

通过R sqlSave更新SQL表

[英]update an SQL table via R sqlSave

I have a data frame in R having 3 columns, using sqlSave I can easily create a table in an SQL database: 我在R中有一个有3列的数据框,使用sqlSave我可以在SQL数据库中轻松创建一个表:

channel <- odbcConnect("JWPMICOMP")
sqlSave(channel, dbdata, tablename = "ManagerNav", rownames = FALSE, append = TRUE, varTypes = c(DateNav = "datetime"))
odbcClose(channel)

This data frame contains information about Managers (Name, Nav and Date) which are updatede every day with new values for the current date and maybe old values could be updated too in case of errors. 此数据框包含有关管理器(名称,导航和日期)的信息,这些信息每天都会使用当前日期的新值进行更新,如果出现错误,也可能会更新旧值。

How can I accomplish this task in R? 我怎样才能在R中完成这项任务?

I treid to use sqlUpdate but it returns me the following error: 我喜欢使用sqlUpdate,但它返回以下错误:

> sqlUpdate(channel, dbdata, tablename = "ManagerNav")
Error in sqlUpdate(channel, dbdata, tablename = "ManagerNav") : 
  cannot update ‘ManagerNav’ without unique column

When you create a table "the white shark-way" (see documentation), it does not get a primary index, but is just plain columns, and often of the wrong type. 当你创建一个表“white shark-way”(参见文档)时,它不会获得主索引,而只是普通的列,而且通常是错误的类型。 Usually, I use your approach to get the columns names right, but after that you should go into your database and assign a primary index, correct column widths and types. 通常,我使用您的方法来获取列名称,但之后您应该进入数据库并分配主索引,正确的列宽和类型。

After that, sqlUpdate() might work; 之后, sqlUpdate()可能会起作用; I say might, because I have given up using sqlUpdate() , there are too many caveats, and use sqlQuery(..., paste("Update....))) for the real work. 我说可能,因为我放弃了使用sqlUpdate() ,有太多警告,并使用sqlQuery(..., paste("Update....)))来实现真正的工作。

What I would do for this is the following 我将为此做的是以下内容

Solution 1 解决方案1

sqlUpdate(channel, dbdata,tablename="ManagerNav", index=c("ManagerNav"))

Solution 2 解决方案2

Lcolumns <- list(dbdata[0,]) 
sqlUpdate(channel, dbdata,tablename="ManagerNav", index=c(Lcolumns))

Index is used to specify what columns R is going to update. 索引用于指定R要更新的列。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM