gpt4 book ai didi

r - R sqlite:使用两个表进行更新会给出语法错误“near”。”:

转载 作者:行者123 更新时间:2023-12-03 18:38:13 26 4
gpt4 key购买 nike

我想用sqlite上的两个表进行UPDATE。

x1 <- data.frame(id = rep(1,3),
t = as.Date(c("2000-01-01","2000-01-15","2000-01-31"))
)
x1.h <- 0
x2 <- data.frame(id = 1, start = as.Date("2000-01-14"))


UPDATE是:

sqldf(paste("UPDATE x1"
," SET x1.h = 1"
," WHERE EXISTS (SELECT *"
," FROM x2"
," WHERE x1.id = x2.id"
," AND x1.t < x2.start"
," )"
)
)


我收到以下错误:

Error in sqliteExecStatement(con, statement, bind.data) : 
RS-DBI driver: (error in statement: near ".": syntax error)


有人知道出什么问题了吗?
感谢您的帮助。

最佳答案

您为什么使用sqldf更新?我认为sqldf对于select语句而言是唯一的选择。

我会为此使用RSQLite

首先,我更正了您的sql状态。我更喜欢使用sep'\ n',与cat获得漂亮的请求

str.update <- paste(" UPDATE x1"
," SET h = 1 " ## here the error
," WHERE EXISTS (SELECT * "
," FROM x2 " ## here second error
," WHERE x1.id = x2.id "
," AND x1.t < x2.start "
," )"
,sep ='\n'
)


cat(str.update)
UPDATE x1
SET h = 1
WHERE EXISTS (SELECT *
FROM x1,x2 ##
WHERE x1.id = x2.id
AND x1.t < x2.start
)


然后,您可以执行以下操作:

library(RSQLite)
con <- dbConnect(SQLite(), ":memory:")
dbWriteTable(con, "x1", x1) # I create my table x1
dbWriteTable(con, "x2", x2) # I create my table x2
res <- dbSendQuery(con, str.update)
dbReadTable(con,name='x1') ## to see the result


编辑

我在Op澄清后编辑答案( FROM x1,x2变为 FROM x2

关于r - R sqlite:使用两个表进行更新会给出语法错误“near”。”:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com