这是我第一次尝试在本地 MySQL 数据库和 R 之间来回传递数据。也就是说,我在数据库中创建了一个表并想将数据插入其中。目前,它是一个空白表(使用 MySQL 查询浏览器创建)并且有一个 PK 集。
我正在使用 RODBC 包(RMySQL 给我错误)并且更喜欢坚持使用这个库。
我应该如何将数据框中的数据插入到此表中?是否有快速解决方案或我是否需要:
- 从我的数据框创建一个新的临时表
- 插入数据
- 删除临时表
使用单独的命令?非常感谢任何帮助!
最佳答案
参见包文档中的help(sqlSave)
;示例显示
channel <- odbcConnect("test")
sqlSave(channel, USArrests, rownames = "state", addPK=TRUE)
sqlFetch(channel, "USArrests", rownames = "state") # get the lot
foo <- cbind(state=row.names(USArrests), USArrests)[1:3, c(1,3)]
foo[1,2] <- 222
sqlUpdate(channel, foo, "USArrests")
sqlFetch(channel, "USArrests", rownames = "state", max = 5)
sqlDrop(channel, "USArrests")
close(channel)
希望这足以让您继续前进。
关于mysql - RODBC 插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910585/