Connect Oracle DB through RStudio using RODBC - r

I might be re-asking the question but I am having difficulty creating connection to Oracle DB which is on a remote server. I am new to R and Oracle DB so I do not have much experience. I am using RStudio. I have installed RODBC package successfully. I am using Ubuntu 14.04 Operating System.
Firstly I use library RODBC
library("RODBC", lib.loc="~/R/x86_64-pc-linux-gnu-library/3.0")
Then I have entered following command:
odbcConnect(dsn = "DatabaseName", uid = "UserID", pwd = "Password")
I get the following error:
[1] -1
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=DatabaseName;UID=UserID;PWD=Password") :
[RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified
2: In RODBC::odbcDriverConnect("DSN=DatabaseName;UID=UserID;PWD=Password") :
ODBC connection failed
I have searched but I am not getting to anywhere. Please tell me process or steps to how I can make a connection.
Thank You!

Related

Can't use Oracle OBDC driver in R in Mac M1 architecture

I am trying to run some queries in Oracle SQL through R using RODBC but it doesn't work, not matter what I try.
I installed the "basic", "sqlplus", "odbc" and "sdk" packages from Oracle (yes, I know sdk and sqlplus aren't strictly needed), did all the path settings needed, copied libodbcinst.2.dylib to the instant client folder, installed the odbc driver in odbcinst.ini, tried changing dynlib locations through install_name_tool, even tried adding "arch -x86_64" before the driver name on odbcinst.ini - all to no avail.
I am using a MacBook Air M1, macOS Monterey 12.3.1, unixodbc installed on opt/homebrew from homebrew...
The connection string is:
sqlDriver <- 'Oracle 19 ODBC driver'
connectionStringSQL <- paste0(
"Driver=", sqlDriver,
";Server=", sqlServer,
";Database=", sqlDatabase,
";Uid=", sqlUser,
";Pwd=", sqlPassword,
";Encrypt=yes",
";Port=1521")
conn <- odbcDriverConnect(connectionStringSQL)
But I don't think the connection string matters, because the error I get is:
Warning messages:
1: In odbcDriverConnect(connectionStringSQL) :
[RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'arch -x86_64 /Users/*****/instantclient/libsqora.dylib.19.1' : file not found
2: In odbcDriverConnect(connectionStringSQL) : ODBC connection failed

Error in connecting R to Oracle database using RODBC

I am trying to connect R to an Oracle database. I am getting an error which I have already tried to trouble shoot but not able to find a solution.
I created a DSN and successfully tested the connection
I then tried to establish a connection in R.
con <- odbcConnect("DSNNAME", uid='ID', pwd='PWD')
But I receive an error.
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=DSNNAME;UID=ID;PWD=PWD") :
[RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver Manager]
The specified DSN contains an architecture mismatch between the Driver and Application
2: In RODBC::odbcDriverConnect("DSN=DSNNAME;UID=ID;PWD=PWD") :
ODBC connection failed
I have replaced my id and password with dummies.
Also I am using 64 bit R.

Query MS Access DB from R with 64-bit Windows

Someone asked me to create a Shiny UI to allow them to read and write from their Microsoft Access database, created with MS Office 2010.
I was following this guide to connect to the Access DB with RODBC and -- while I can open this database in Access itself -- I get the following error from R:
> channel <- odbcConnectAccess("AD_Users.accdb")
Error in odbcConnectAccess("AD_Users.accdb") : `
odbcConnectAccess is only usable with 32-bit Windows
So, I found this solution and gave it a try:
> channel <- odbcDriverConnect("AD_Users.accdb")
Warning messages:
1: In odbcDriverConnect("AD_Users.accdb") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect("AD_Users.accdb") :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC Driver Manager] Invalid connection string attribute
3: In odbcDriverConnect("AD_Users.accdb") : ODBC connection failed
> channel <- odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=AD_Users.accdb")
Warning messages:
1: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=AD_Users.accdb") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=AD_Users.accdb") :
ODBC connection failed
>
but as you can see, there were some pretty serious warnings. It doesn't seem to work.
Is there any other work-around?
I am using 32-bit R when I receive these warnings and even though they're warnings and not technically errors, the database connection is unsuccessful.
Update
This seems to establish a successful connection, though I've been unable to query data from it thusfar:
channel <- odbcConnectDbase("AD_Users.accdb")
I use odbcConnectAccess2007 in RODBC and don't have any issues connecting to Access databases when using 64-bit windows. However, you should check the package manual (link to pdf) to make sure that you have the appropriate drivers installed on your computer. Once you have the right drivers you should be good to go!

Connecting to Oracle DB (on remote server) using RODBC - R

I'm pretty new to R and may be I'm re-asking this question again, but I'm posting this as I didn't find an appropriate answer.
I'm trying to connect to Oracle DB that sits on a remote location from R using RODBC. I went through a lot of RODBC configuration documents but nothing clearly specifies connection to remote Oracle DB and what is the root cause.
I also pondered on using ROracle as DBI but found out that RODBC fares much better for multiple reasons.
So, here's my code:
> # Load RODBC package
> library(RODBC)
> # Create a connection to the database
> dbconnection <- odbcDriverConnect("Driver={Oracle ODBC Driver}; Server=my_hostname; Database=my_db_name; Uid=my_uid; Pwd=my_pwd")
> # Check that connection is working
> odbcGetInfo(dbconnection)
> close(dbconnection)
And the TNS Config that I use [values altered for obvious reasons]:
my_db_name =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = my_hostname)(PORT = my_port))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = my_service_name)
)
)
Note: The other thing here is that I don't have a local Oracle DB setup. I use SQL Developer to connect to the remote Oracle DB with the given TNS Config.
I get the below error:
Warning messages:
1: In odbcDriverConnect("Driver={Oracle ODBC Driver}; Server=my_hostname; Database=my_db_name; Uid=my_uid; Pwd=my_pwd") :
[RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'Oracle ODBC Driver' : file not found
2: In odbcDriverConnect("Driver={Oracle ODBC Driver}; Server=my_hostname; Database=my_db_name; Uid=my_uid; Pwd=my_pwd") :
ODBC connection failed
After installing R, I have only installed RODBC using the install.packages("RODBC") command. Also, I'm running this on Mac OS.
I know I'm missing something here. Specifically the Driver={Oracle ODBC Driver} might need to modified to the actual drivers name. But I don't know what it should be and googling didn't help much.
Is there anything else that I need to install or configure?

Connect R and Vertica using RODBC

This is my first time connecting to Vertica. I have already connected to a MySQL database sucessfully by using RODBC library.
I have the database setup in vertica and I installed the windows 64-bit ODBC driver from https://my.vertica.com/download-community-edition/
When I tried to connect to vertica using R, I get the below error:
channel = odbcDriverConnect(connection = "Server=myserver.edu;Database=mydb;User=mydb;Password=password")
Warning messages:
1: In odbcDriverConnect(connection = "Server=myserver.edu;Database=mydb;User=mydb;Password=password") :
[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 = "Server=myserver.edu;Database=mydb;User=mydb;Password=password") :
ODBC connection failed
Can someone tell me how to fix this? Or is there any other ways to connect to vertica using R?
It may not be the fastest, but I prefer to use the Vertica JDBC driver from R. Getting the ODBC drivers working is a little messy across different operating systems. If you already have a Java Runtime Environment (JRE) installed for other applications then this is fairly straightforward.
Download the Vertica JDBC drivers for your Vertica server version from the MyVertica portal. Place the driver (a .jar file) in a reasonable location for your operating system.
Install RJDBC into your workspace:
install.packages("RJDBC",dep=TRUE)
In your R script, load the RJDBC module and create an instance of the Vertica driver, adjusting the classPath argument to point to the location and filename of the driver you downloaded:
library(RJDBC)
vDriver <- JDBC(driverClass="com.vertica.jdbc.Driver", classPath="full\path\to\driver\vertica_jdbc_VERSION.jar")
Make a new connection using the driver object, substituting your connection details for the host, username and password:
vertica <- dbConnect(vDriver, "jdbc:vertica://host:5433/db", "username", "password")
Then run your SQL queries:
myframe = dbGetQuery(vertica, "select Address,City,State,ZipCode from MyTable")
You have to use double slash in the classPath arguement in JDBC function.
for example,
vDriver <- JDBC(driverClass="com.vertica.jdbc.Driver",
classPath="C:\\Program Files\\Vertica Systems\\JDBC\\vertica-jdk5-6.1.2-0.jar")
worked for me, while just copying and pasting the route failed.

Resources