gpt4 book ai didi

mysql - RODBC 插入查询

转载 作者:行者123 更新时间:2023-11-29 04:58:48 26 4
gpt4 key购买 nike

这是我第一次尝试在本地 MySQL 数据库和 R 之间来回传递数据。也就是说,我在数据库中创建了一个表并想将数据插入其中。目前,它是一个空白表(使用 MySQL 查询浏览器创建)并且有一个 PK 集。

我正在使用 RODBC 包(RMySQL 给我错误)并且更喜欢坚持使用这个库。

我应该如何将数据框中的数据插入到此表中?是否有快速解决方案或我是否需要:

  1. 从我的数据框创建一个新的临时表
  2. 插入数据
  3. 删除临时表

使用单独的命令?非常感谢任何帮助!

最佳答案

参见包文档中的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/

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