R ODBC MySQL Connection Example

放肆的年华 提交于 2019-12-05 07:21:28

问题


I'm trying to use RODBC to connect to a MySQL db on my computer (I'm assuming it's localhost). I've read the package reference manual and can't figure out how to do anything (connect, set default driver, open channel, etc). Any suggestions?

EDIT:

> install.packages("RMySQL", type="source")
Installing package(s) into ‘C:/Users/backupSam/Documents/R/win-library/2.13’
(as ‘lib’ is unspecified)
trying URL 'http://lib.stat.cmu.edu/R/CRAN/src/contrib/RMySQL_0.8-0.tar.gz'
Content type 'application/x-gzip' length 160735 bytes (156 Kb)
opened URL
downloaded 156 Kb

* installing *source* package 'RMySQL' ...
ERROR: configuration failed for package 'RMySQL'
* removing 'C:/Users/backupSam/Documents/R/win-library/2.13/RMySQL'
* restoring previous 'C:/Users/backupSam/Documents/R/win-library/2.13/RMySQL'

The downloaded packages are in
        ‘C:\Users\backupSam\AppData\Local\Temp\RtmpitXEFu\downloaded_packages’
Warning messages:
1: running command 'C:/PROGRA~1/R/R-213~1.2/bin/x64/R CMD INSTALL -l "C:/Users/backupSam/Documents/R/win-library/2.13"   C:\Users\BACKUP~1\AppData\Local\Temp\RtmpitXEFu/downloaded_packages/RMySQL_0.8-0.tar.gz' had status 1 
2: In install.packages("RMySQL", type = "source") :
  installation of package 'RMySQL' had non-zero exit status

回答1:


First set up a connection.

1) For me I had to download a driver on MySql's website, which will vary by system and version, I used this page:

Windows ODBC Drivers

2) Once this is downloaded run the setup utility.

3) Next setup up the DSN. Instructions for windows are here: MySQL ODBC DSN Setup

4) Important: Remember the name of the DSN as it is used when you create the channel in RODBC to connect to your database.

5) Finally, once this is setup you install and load the RODBC package.

6) To connect to your database use something like this:

channel <- odbcConnect("mysql 2", uid="root")

where 'mysql 2' is the name of your DSN connection, NOT the name of the database.

7) Finally you can send a query like this:

result1 <- sqlQuery(channel, paste("SELECT * from db1"))


来源:https://stackoverflow.com/questions/7983770/r-odbc-mysql-connection-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!