首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R SQLITE表升级

R SQLITE表升级
EN

Stack Overflow用户
提问于 2021-10-19 20:36:30
回答 1查看 21关注 0票数 0

我想要更新表,并且我正在R中使用SQLite。我得到一个接近“良好”的systax错误。我找不到问题所在。

代码语言:javascript
复制
library(RSQLite)
library(ggplot2)
View(diamonds)
conn <- dbConnect(RSQLite::SQLite(),"diamonds.db")

dbListTables(conn)
dbWriteTable(conn, "pqr", diamonds,overwrite = TRUE)
dbListTables(conn)
d_1 <-dbGetQuery(conn, "SELECT * FROM pqr")

d_2 <- d_1[10,]

v_carat <- d_2$carat
v_cut <- d_2$cut
v_color <- d_2$color
#high test quality to be updated in clarity
this_p <- "high test quality"


dbExecute(conn, sprintf("UPDATE pqr SET clarity = '%s' where carat =%f and cut =%s and color =%s" ,
                                    this_p, v_carat,v_cut,v_color))

请指点一下。谢谢并致以问候,R

EN

回答 1

Stack Overflow用户

发布于 2021-10-19 20:41:00

这两个字符串未加引号

代码语言:javascript
复制
> sprintf("UPDATE pqr SET clarity = '%s' where carat =%f and cut =%s and color =%s" ,
+                                     this_p, v_carat,v_cut,v_color)
[1] "UPDATE pqr SET clarity = 'high test quality' where carat =0.230000 and cut =Very Good and color =H"

如果我们让它们成为'H''Very Good',它应该可以工作

代码语言:javascript
复制
 dbExecute(conn, sprintf("UPDATE pqr SET clarity = '%s' where carat =%f and cut ='%s' and color ='%s'" ,
                                     this_p, v_carat,v_cut,v_color))
[1] 8

-checking

代码语言:javascript
复制
>  d_1 <-dbGetQuery(conn, "SELECT * FROM pqr")
> 
> d_1[10,]
   carat       cut color           clarity depth table price x    y    z
10  0.23 Very Good     H high test quality  59.4    61   338 4 4.05 2.39

-checking数据

代码语言:javascript
复制
> library(dplyr)
> d_1 %>%
    filter(clarity == this_p, cut == v_cut, color == v_color)
  carat       cut color           clarity depth table price    x    y    z
1  0.23 Very Good     H high test quality  59.4    61   338 4.00 4.05 2.39
2  0.23 Very Good     H high test quality  61.0    57   353 3.94 3.96 2.41
3  0.23 Very Good     H high test quality  62.4    55   431 3.91 3.94 2.45
4  0.23 Very Good     H high test quality  62.6    56   431 3.92 3.95 2.46
5  0.23 Very Good     H high test quality  61.6    56   434 3.94 3.97 2.43
6  0.23 Very Good     H high test quality  63.9    55   369 3.89 3.90 2.49
7  0.23 Very Good     H high test quality  62.0    55   378 3.93 3.95 2.44
8  0.23 Very Good     H high test quality  59.2    61   389 4.00 4.04 2.38
> d_1 %>%
    filter(clarity != this_p) %>%
      head
  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69637375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档