如果数据库中已经存在值,则R跳到下一条记录
Posted
技术标签:
【中文标题】如果数据库中已经存在值,则R跳到下一条记录【英文标题】:R Skip to Next Record if Value Already Exists in Database 【发布时间】:2016-05-02 19:21:59 【问题描述】:我在这里撞墙试图解决这个问题......我有一个脚本,如果我正在写的表中已经存在 game_id,我想跳到下一条记录循环
#Connect to db
saber <- odbcConnect("sabermetrics")
#query to pull all existing game records
check_query <- paste0("select distinct game_id from allbats")
#build variables to compare db field and game_id in current url
game_id_check <- sqlQuery(saber, check_query)
curr_gameid <- substr(thisboxscoreURL, 66, 95)
#if they match sktip to next record
if(game_id_check == curr_gameid) next
我尝试将数据矩阵化、列出、取消列出、组合以使数据“匹配”,但到处都失败了!
我可能会将来自 url 的 game_id 放在查询字符串的 where 子句中,如果计数大于 0,则也可以跳到下一条记录。我不确定哪个会更好,可能是后者,因为我最多只会为循环中的每条记录返回一个值。
类似
#Connect to db
saber <- odbcConnect("sabermetrics")
#query to pull all existing game records
check_query <- paste0("select distinct game_id from allbats where game_id = url_game_id")
if(count(sqlquery(saber, check_query)))>0 next
示例数据
away home inning away_team_code home_team_code game_id
0 0 1 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 2 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 3 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 4 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 1 5 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 6 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 7 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 0 8 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
0 x 9 sdn sfn gid_2016_04_26_sdnmlb_sfnmlb_1
这是 dput,但不确定它是否有用。
dput(sqlQuery(saber, "SELECT TOP 20 * FROM allbats"))
structure(list(away = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 5L, 0L, 1L, 0L, 0L, 0L), home = structure(c(1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("0", "1", "2", "x"), class = "factor"),
inning = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L), away_team_code = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L), .Label = c("hou", "mia", "sdn"), class = "factor"),
home_team_code = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("lan",
"sea", "sfn"), class = "factor"), game_id = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L), .Label = c("gid_2016_04_26_houmlb_seamlb_1",
"gid_2016_04_26_miamlb_lanmlb_1", "gid_2016_04_26_sdnmlb_sfnmlb_1"
), class = "factor")), .Names = c("away", "home", "inning",
"away_team_code", "home_team_code", "game_id"), row.names = c(NA,
20L), class = "data.frame")
任何帮助将不胜感激!!谢谢!
【问题讨论】:
您使用的是哪个数据库后端(SQL Server、SQLite、...)? 我在后台使用sql server 只有当新记录的game_id
不存在于表中时,您才尝试将新记录插入数据库表中?另外,您能否提供一个数据样本来阐明表格的结构(例如,将dput(sqlquery(saber, "SELECT TOP 20 * FROM allbats"))
的输出粘贴到您的问题中)?
这正是我想要做的,如果表中不存在 game_id 则插入。我将示例数据附加到我的原始帖子中。感谢您的帮助@nrussell!
【参考方案1】:
我想出了一个使用 sqldf 包的方法
saber <- odbcConnect("sabermetrics")
existingGames <- paste0("select distinct game_id from linescore")
newGames <- data.frame(gameids)
existingGames <- sqlQuery(saber, existingGames)
require(sqldf)
# Get all new games that do not exist in existing games (linescore table)
ngNotIneg <- sqldf('SELECT distinct * FROM newGames EXCEPT SELECT distinct * FROM existingGames')
odbcClose(saber)
【讨论】:
以上是关于如果数据库中已经存在值,则R跳到下一条记录的主要内容,如果未能解决你的问题,请参考以下文章
如果 Notepad.exe 正在运行,则 taskkill 如果未运行,则转到下一条语句