如何将R中的数据框导出到MySQL中的表

发布于 2024-12-16 15:58:16 字数 56 浏览 4 评论 0原文

我在 RODBC 中尝试了 sqlSave(),但它运行速度非常慢。有没有其他方法可以做到这一点?

I tried sqlSave() in RODBC, but it runs super slowly. Is there an alternative way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-12-23 15:58:16

您可以查看包RMySQL。我正在使用它,它提供了相当方便的从 MySQL 数据库加载和读取数据的功能。话虽这么说,它在您可以使用的查询中是有限的(例如,HAVING 是不可能的 IIRC)。我不能说它非常快,或者我的数据有那么大,但它是几个 2 位 MB 的文本,而且还可以。取决于您的期望。不过它很方便:

con <- dbConnect(MySQL(), user="user", password="pass", 
    dbname="mydb", host="localhost",client.flag=CLIENT_MULTI_STATEMENTS)

dbListTables(con)
yourtable <- dbReadTable(con,"sometable")
# write it back
dbWriteTable(con,"yourTableinMySQL",yourtable,overwrite=T)
# watch out with the overwrite argument it does what it says :)
dbDisconnect(con)

yourtable 将是一个 data.frame。有时,模式没有像我期望的那样设置,这让我很烦恼,但我有一个定制的函数。只需要改进它,然后我将其发布在这里。

http://cran.r-project.org/web/packages/RMySQL/

You could look at the package RMySQL. I am using it and it offers quite some convenience loading and reading data from a MySQL database. That being said it is limited in the queries you can use (e.g. HAVING is not possible IIRC). I can't say it's super-quick or my data is that big, but it's several 2-digits MB of text and it's ok. Depends on what you expect. However it's convenient:

con <- dbConnect(MySQL(), user="user", password="pass", 
    dbname="mydb", host="localhost",client.flag=CLIENT_MULTI_STATEMENTS)

dbListTables(con)
yourtable <- dbReadTable(con,"sometable")
# write it back
dbWriteTable(con,"yourTableinMySQL",yourtable,overwrite=T)
# watch out with the overwrite argument it does what it says :)
dbDisconnect(con)

yourtable will be a data.frame. Sometimes it bugs me that the modes are not set like i'd expect, but I have a custom made function for that. Just need to improve it then I'll post it here.

http://cran.r-project.org/web/packages/RMySQL/

╄→承喏 2024-12-23 15:58:16

在尝试安装 RMySQL 之前,您需要安装 mysql 客户端代码。在不知道您使用的操作系统和版本的情况下,实际上不可能给出更好的答案。

You need to have mysql client codes installed before you try installing RMySQL. Without knowing what OS and version you use it is really not possible to give any better answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文