How to Connect R to Oracle? - database

I need to connect R to oracle and I have been unsuccessful so far. I downloaded two packages: RODBC & RODM.
This is the statement that I've been using:
DB <- odbcDriverConnect("DBIORES1",uid="mhala",pwd="XXXXXXX")
But I get this error:
Error in odbcDriverConnect("DBIORES1", uid = "mhalagan", pwd = "XXXXXXX") :
unused argument(s) (uid = "mhalagan", pwd = "XXXXXXX")
What information do I need to be able to connect to an oracle database? Am I using the correct package?

See the help page for odbcDriverConnect(). odbcDriverConnect() does not accept uid or pwd arguments. You probably meant to use odbcConnect() instead:
odbcConnect(dsn = "DBIORES1", uid = "mhala", pwd = "XXXXXXX")
In addition to the RODBC package, there is the RODM package, which I believe is specifically designed for Oracle databases and is further described here: http://www.oracle.com/technetwork/articles/datawarehouse/saternos-r-161569.html . I do not use Oracle databases, so cannot comment on advantages of the two packages.

RJDBC worked just fine for me. You just need to have Oracle-thin driver jar file and configure the connection like:
> install.packages("RJDBC")
> library(RJDBC)
> drv <- JDBC("oracle.jdbc.driver.OracleDriver","/path/to/driver/com/oracle/oracle-thin/11.2.0.1.0/oracle-thin-11.2.0.1.0.jar”)
> conn <- dbConnect(drv, "jdbc:oracle:thin:#database:port:schema”, “user”, “passwd”)
and then is ready to perform some queries.
JA.

I've had success in the past connecting to Oracle databases from R with RJDBC. I found it easier to get going as I just grabbed the connection string that I'd used successfully inside the java based GUI I was using at the time and like magic it "just works"(tm).

Did you install the oracle ODBC client/driver? You will need that if you are going to use the ODBC R package. Go to oracle instant client download get the client for your OS. install them and then proceed to configure the ODBC and test the connection outside of R then install the R and RODBC and test inside R.

Related

Not able to use RSQLserver

I am trying to use RSQLServer and first installed RSQLServer using R studio. then I am trying to use it like this.
library(RSQLServer)
library(DBI)
drv <- dbDriver("SqlServer")
conn <- dbConnect(drv, url = "Server=**MYSERVERURL;database=DBName;trusted_connection=yes;")
res <- dbSendQuery(conn, "SELECT TOP 100 * FROM test_table (NOLOCK)")
str(res)
But I am getting error everytime. Am I missing something? The Error is Object not found.? Do I need to configure any driver (probably jTDS) first? If yes, can anyone share steps to do that? Thanks.
Error text
> conn <- dbConnect(drv, url = "Server=**MYSERVERURL;database=DBName;trusted_connection=yes;")
Error in dbConnect(drv, url = "Server=**MYSERVERURL;database=DBName;trusted_connection=yes;") :
object 'drv' not found
> res <- dbSendQuery(conn, "SELECT TOP 100 * FROM test_table (NOLOCK)")
Error in dbSendQuery(conn, "SELECT TOP 100 * FROM test_table (NOLOCK)") :
object 'conn' not found
> str(res)
Error in str(res) : object 'res' not found
Note: name of table and database changed.
Try to use
drv <- RSQLServer::SQLServer()
instead of
drv <- dbDriver("SqlServer")
You must have downloaded and installed the jTDS driver.
For Windows authentication you have to install a DLL too:
If you intend to use integrated security (Windows Authentication) to
authenticate your server session, you will need to download jTDS and
copy the native single sign on library (ntlmauth.dll) to any
location on your system’s PATH (e.g. Sys.getenv("PATH") ).
Source: https://cran.r-project.org/web/packages/RSQLServer/RSQLServer.pdf
Your JDBC connection string looks strange, please make sure your JDBC connection string is correct.
If you are using the jTDS driver the connection string syntax is
different from the JDBC driver of Microsoft
The jTDS syntax is specified here:
http://jtds.sourceforge.net/faq.html#urlFormat
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
where is "sqlserver".
The Microsoft JDBC syntax is specified here but I think it does not work because RSQLServer is based on the cross-platform jTDS JDBC driver
https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx
Example:
jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;
Replace the "localhost" part with the IP address or server name like "myServer.honey.moon.com", in case of a non-standard IP port (not 1433) of the instance use "localhost:1234".
You can figure out the IP port by looking at the connection string you use to connect to the database via SQL Server Management Studio!

How do I connect to an SQL server database in R

I'm trying to connect to the SQL Sever database using R but not sure on the details for the query string. I normally use SQL server management studio on SQL Server 2008 and connnect using single sign on. I found the below example
myconn <- odbcDriverConnect(connection="Driver={SQL Server
Native Client 11.0};server=hostname;database=TPCH;
trusted_connection=yes;")
I get the below warning message
Warning messages:
1: In odbcDriverConnect(connection = "Driver={SQL Server \nNative Client 11.0};server=hostname;database=TPCH;\ntrusted_connection=yes;") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect(connection = "Driver={SQL Server \nNative Client 11.0};server=hostname;database=TPCH;\ntrusted_connection=yes;") :
ODBC connection failed
How do I go about finding the specifics i need?
I have done this in the past with an odbc named connection that I've already had in place. In case you don't know, you can create one in windows by typing into the search prompt 'odbc' and selecting "set up data sources". For example - if you named an odbc connection 'con1' you can connect the following way:
con<-odbcConnect('con1') #opening odbc connection
df<-sqlQuery(con, "select *
from ssiD.dbo.HOURLY_SALES
") #querying table
close(con)
This works for me.
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=server_name; Database=table_name;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)
Also, see these links.
RODBC odbcDriverConnect() Connection Error
https://www.simple-talk.com/sql/reporting-services/making-data-analytics-simpler-sql-server-and-r/
The problem is simpler than this. The big clue is the \n in the error message. Something has re-flowed your connection string such that there is now a new-line character in the driver name. That won't match any registered driver name. Pain and suffering then ensues. Make sure your whole connection string is on a single line!
I often use:
driver={SQL Server Native Client 11.0}; ...
and it works really well. Much better than having to rely on pre-defined connection names.
Try another ODBC driver.
In windows press the "windows" button and then type "odbc".
Click the "Data sources (ODBC)" link.
Go to the "Drivers" tab to see the available drivers for SQL Server.
Also - remove the " " spaces after the semicolons in your connection string.
Note - the database property should point to a database name rather than a table name.
This worked for me:
odbcDriverConnect("Driver=SQL Server Native Client 11.0;Server=<IP of server>;Database=<Database Name>;Uid=<SQL username>;Pwd=<SQL password>")
First, you need to install the package 'RSQLServer', and all its dependencies.
Then execute the following command in RStudio, with relevant parameters:
conn <- DBI::dbConnect(RSQLServer::SQLServer(),
server = '<server>',
port = '<port>',
properties = list(
user = '<user>',
password = '<password>'
))
Finally, db_list_tables(conn) gives you the list of tables in the corresponding database.

Connecting R to an MS SQL database on a Mac using RODBC

I'm trying to connect to an MS SQL database from R (on a Mac) - after fiddling a lot with odbc.ini, odbcinst.ini, and installing freeTDS as described:
sudo port install freetds +mssql +odbc +universal
it now works on the Mac's command line level, but when trying to access it from R using the command:
con <- odbcConnect("myDSN", uid = "myID", pwd = "myPWD")
it just hangs and when forced to stop executing, I get 50+ of the following warnings:
In odbcDriverConnect("DSN=myDSN;UID=myID;PWD=myPWD") :
[RODBC] ERROR: state IM002, code 1421220112, message [iODBC][Driver Manager]Data source
name not found and no default driver specified. Driver could not be loaded
After having tried to make it work for about two days, I'm running out of suggestions. Can anybody help point me to what I am missing?
EDIT: It also works when running R on the virtual Windows machine. How do I get it to work on the Mac?
Did you first configure your MS SQL driver connection? If you have, then you should have a data source called "myDSN" in the OCBC Data Source dialog box.
Here is a great blog which gives step-by-step instructions and screen captures for what you need to do.
When you issue queries in R, R will try to talk to the ODBC data source called "myDSN". That data source knows what database you want (MS SQL) and also what the credentials (username/password) are needed to get access. This is the reason why you must configure the data source.

Reading data from Microsoft SQL Server into R

Is it possible to read the data stored in MS SQL server from R interface?
If it is I would be also thankful if anyone could show the way to do it.
Tried the RODBC package already?
http://cran.r-project.org/web/packages/RODBC/index.html
There's also the RJDBC package : http://www.rforge.net/RJDBC/
See also :
http://www.r-bloggers.com/connecting-to-sql-server-from-r-using-rjdbc/
I've applied RODBC function suggested by other users. LinkSQL Server RODBC Connection
library(RODBC)
dbhandle <- odbcDriverConnect('driver={SQL
Server};server=mysqlhost;database=mydbname;trusted_connection=true')
res <- sqlQuery(dbhandle, 'select * from information_schema.tables')
change two variables based on your Data table. 'server=mysqlhost;database=mydbname'
Niko, What operating system are you running? The answer to your question varies, depending on the platform you are using.
If you are using Windows (of any stripe), connecting to MSSQL Server via ODBC (RODBC) makes the most sense. When I connect to a MSSQL Server on Linux, I use JDBC as suggested by Joris. I would assume that JDBC is also the best solution for Macs, but I could very well be wrong.
There another option that seems to outperform RODBC and RJDBC
rsqlserver package written by agstudy.
Installation:
require(devtools)
install_github("rClr", 'jmp75')
install_github('rsqlserver', 'agstudy',args='--no-multiarch')
The latest library that allows you to connect to MSSQL databases is RSQLServer.
It can be found on GitHub and CRAN.
You can connect to SQL server using DBI package, which I think works better than RODBC. DBI is a database interface package for relational databases. for SQL I use it along with odbc package as in the example below.
Visit this page for full details: Database Queries with R
An example would be as follows
library(DBI)
library(odbc)
con <- dbConnect(odbc::odbc(), .connection_string = "driver={SQL Server}; server= ServerName; database=DatabaseName; trusted_conncetion=true"))
dbGetQuery(con,'Select * from Table')
library("RODBC")
dbhandle <- odbcDriverConnect('driver={SQL Server};server=;database=;trusted_connection=true')
currTableSQL<-paste("SELECT *
FROM ",sep="")
currTableDF<-sqlQuery(dbhandle,currTableSQL)

Cron Job with R and SQL Server

This is probably going to be an underspecified question, as I'm not looking for a specific fix:
I want to run a machine learning algorithm on some data in a SQL Server database. I'd like to use R to do the calculations -- which would involve using R to connect to the database, process the data, and write a table of results back to the database.
Is this possible? My guess is yes. Shouldn't be a problem using a client...
however, would it be possible to set this up on a linux box as a cron job?
Yes to all!
Your choices for scripting are either Rscript or littler as discussed in this previous post.
Having struggled with connecting to MSSQL databases from Linux, my recommendation is to use RJDBC for database connections to MSSQL. I used RODBC to connect from Windows but I was never able to get it working properly in Linux. To get RJDBC working you will need to have Java installed properly on your Linux box and may need to change some environment variables (seems I always have SOMETHING mis-configured with rJava). You will also need to download and install the JDBC drivers for Linux which you can get directly from Microsoft.
Once you get RJDBC installed and the drivers installed, the code for pulling data from the database will look something like the following template:
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/etc/sqljdbc_2.0/sqljdbc4.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://mySqlServer", "userId", "Password")
sqlText <- paste("
SELECT *
FROM SomeTable
;")
myData <- dbGetQuery(conn, sqlText)
You can write a table with something like
dbWriteTable(conn, "myData", SomeTable, overwrite=TRUE)
When I do updates to my DB I generally use dbWriteTable() to create a temporary table on my database server then I issue a dbSendUpdate() that appends the temp table to my main table then a second dbSendUpdate() that drops the temporary table. You might find that pattern useful.
The only "gotcha" I ran into was that I could never get a Windows domain/username to work in the connection sequence. I had to set up an individual SQL Server account (like sa).
You may just write a script containing R code and put this in the first line:
#!/usr/bin/env Rscript
change the file permissions to allow execution and put in crontab as it would be a bash script.

Resources