开发者

How do I connect to an Oracle Database in R?

I am trying to use the RODBC package to query data from an Oracle database using R. There seems to be a great deal of documentation on accessing MySQL databases from R, but not much information on Oracle databases.

开发者_如何学编程

How do I create a DSN for my Oracle Database?


Here are the instructions I developed for our site:

  1. Install Oracle instant client. The files to extract are: instantclient-basic-win32-11.1.0.7.0.zip instantclient-odbc-win32-11.1.0.7.0.zip instantclient-sqlplus-win32-11.1.0.7.0.zip (optional)

Note: The Basic Instant Client and ODBC Instant Client packages must be unzipped into same directory. For example, C:\Oracle\instantclient_11_1.

Oracle download site: http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

  1. Add the location of the extracted files to the path environment variable (e.g. C:\oracle\instantclient_11_1) a. Right click "My Computer" on the desktop, click "Manage." b. Right click "Computer Management (Local)", click "Properties." c. On the advanced tab, click the Settings button under "Environment Variables." d. Find the "Path" entry under "System variables" and click the Edit button. e. Add the location path to the end of the "Variable Value" box. Note that items are separated by a semi-colon. f. Click OK to confirm changes. Close the Computer Management window.

  2. Double click the odbc_install.exe file in the Instant client directory.

  3. Open C:\WINDOWS\system32\odbcad32.exe

  4. Add a data source for the oracle database. a. Click the Add button b. Select "Oracle in instantclient11_1" and click Finish. c. Enter the following in the Oracle ODBC Driver Configuration dialog: Data Source Name: DSN Description: Roacle (or whatever) TNS Service Name: SERVER:1521/DSN - Change this User ID: Your oracle user name d. Click the Test Connection button. You will be prompted for your password, and if everything went well, you will get a "Connection successful" message.

You can then create a channel in R with:

     odbcConnect(dsn, uid = uid, pwd = pwd, readOnly = TRUE)


I'm not familiar with "R", however:

Oracle ODBC DSN is generally set up by -

  1. installing the instant client
  2. using the windows ODBC manager to create the DSN


I found that there are actually some challenges with connecting to Oracle from R. It might just be the implmentation and settings that we have at my company but here is what I did to get it working.

library(RODBC)

ch=odbcConnect("<AliasForYourDatabase>",pwd = "xxxxxxxx", believeNRows=FALSE)

odbcGetInfo(ch)

The critical step for me was 'believeNRows=FALSE' otherwise I received the following error when I submitted a query:

"Error in .Call(C_RODBCFetchRows, attr(channel, "handle_ptr"), max, buffsize, : negative length vectors are not allowed"

This is because Oracle is not providing the actual number of rows back to the connection.


In case anyone else stumbles on this old question and needs help connecting Oracle and R, this PDF explains the process fully and provides needed download information.

{http://cran.fhcrc.org/web/packages/RODM/RODM.pdf}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜