How do I set up the ODBC connection in R? - r

This is a follow up question to this thread.
Writing data from an R data-set into Redshift
How do I set up the ODBC connection in R? In other words, where I enter the details such as
driver, url and port?

Following the referenced question, if you are on Windows, you create a user DSN or system DSN ODBC connection in the ODBC data source administrator (search for ODBC in the search box of the start button). Then you use this name on the second line of the following code. Where it says "mydsn".
library(RODBC)
myconn <-odbcConnect("mydsn", uid="Rob", pwd="aardvark")
crimedat <- sqlFetch(myconn, "Crime")
pundat <- sqlQuery(myconn, "select * from Punishment")
close(myconn)
If you haven't got the Amazon Redshift ODBC drivers installed, you will have to install them first. I found a link here.

Related

Connecting to Snowflake using RODBC package in R

Can anyone provide the connection string for connecting to Snowflake through RODBC:: odbcconnect in R?
Thanks in advance!
I'm moving from Teradata to Snowflake. And I have a lot of functions in my code which works only with an RODBC connection, but I'm only finding DBI connections to Snowflake on the internet.
Based on your question I assume that you have already installed the Snowflake ODBC driver and created a DSN following the Snowflake documentation appropriate for your OS and you have installed RODBC package working with other ODBC DSNs in your system.
Then you can connect to Snowflake as follows:
library(RODBC)
ch <- odbcConnect("DSN_NAME", uid = "USERNAME", pwd = "PASSWORD")
#test query to test the connection by printing the current timestamp
sqlQuery(ch, paste('SELECT CURRENT_TIMESTAMP()'))

Shiny App Connect dbPool through ODBC or specific driver

I'm struggling to connect my shiny application to one of the database we use in our company. I've succesfully connected to Azure / Mongo / SQL Server databases but now I've got a SAP SQL Anywhere 17 database to connect to.
Not surprisingly there's no specific connection to that database provided in the R Drivers (https://www.rstudio.com/products/drivers/).
Now I can solve this in two ways I believe, our IT department is convinced that a generic ODBC connection should work, or I have to get the specific SQL Anywhere drivers installed on my shiny app somehow.
For both solutions I can't find much online. If I search for generic ODBC connection the recommendations go to FreeTDS which is in the RODBC package, which then might not work together with Pool (according to what I've read).
Searching how to install specific drivers on a shiny application is also not bringing me much.
Try using
options(java.parameters="-Xmx8g")
library(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "drivers/jdbc/mssql-jdbc-8.4.1.jre8.jar", identifier.quote="`")
processingStart = Sys.time()
conn <- dbConnect(drv,
"jdbc:sqlserver://SERVER_NAME;databaseName=DATABASE_NAME",
user = "USER_NAME",
"PASSWORD"
)

R to Oracle Connection Using ODBC

I am trying to make a connection from R to Oracle and I am having issues doing so.
Here is the connection script I am using with the DBI and odbc packages:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "Oracle in OraClient11g_home1",
Server = "orasade06.hc-sc.gc.ca",
SID = "sdv11040",
Schema = "STG",
Username = "username",
Password = "pw",
Port = 1521)
I am able to make a successful connection through Oracle SQL Developer to view my database and schema.
I have installed the Oracle ODBC driver (Oracle in OraClient11g_home1) on my machine, however I cannot make a successful connection from "R".
Here is the error I get:
Error: nanodbc/nanodbc.cpp:983: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any help and guidance would be greatly appreciated.
Thanks.
I have written a tutorial around this, maybe take a look :
https://guillaumepressiat.github.io/blog/2019/11/oraclyr
Some key points :
If your Oracle client is 32-bit, using R 32-bit is a (or the) solution. Otherwise R and odbc error messages aren't crystal-clear
In RStudio see if an Oracle client appears in Connections Pane
It's easier on linux but strings and parameters for odbc are a little different.
Lastly, for connection string, this answer is interesting : how to make a connection string for oracle that includes hostname, instance name, user id, password using system.data.oracleclient?

Connecting to an Azure SQL Server Data Warehouse from R on a Mac - See random names instead of tables

I'm trying to connect to an Azure SQL Server (12.00.1900) from R on a Mac, using Microsoft's unixodbc SQL Server drivers (17).
I get a connection, but instead of seeing the 12 or so tables that live in the database, dbListTables returns 442 tables, all with nonsensical names, beginning with 'Csoe', 'Ote', and ending in 'xlshm_idad'. Instead of seeing the single schema that lives in the database, I see cin_1mro__e, IFRAINSHM, and s, none of which have any tables in them.
Note that when I use an ordinary SQL visualization app, that doesn't use the MS drivers, I'm able to see the tables and their content properly.
In addition, the RSQLServer package gets a working connection and sees the tables correctly, but isn't compatible with dplyr semantics.
Can anyone help or advise? I've looked for third party SQL Server unixodbc drivers for Mac, and I can't find any.
Until I see more info from OP, I'll leave as my answer the general recommendation to use R's odbc package. Assuming the correct drivers are installed, connection is configured correctly in odbc.ini, and assuming trusted_connection=yes is used in the same, then connecting from R is as simple as:
library(odbc)
dbConn <- dbConnect(odbc(), dsn = "myDSN")
if trusted connection is not on then you just need to pass uid and pwd arguments.
Also, it may be the case OP that you did not install freeTDS, so try (replace with equivalent for package manager you're using):
brew install freetds --with-unixodbc
This gives you the libtdsodbc.so driver. Make sure the DSN points to this.

R cassandra odbc error

when i am linking up R and Cassandra with the RODBC library, i am getting below error
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=DSN=/usr/share/cassandra/lib/cdata.jdbc.cassandra.jar")
:
[RODBC] ERROR: state IM012, code 0, message [unixODBC][Driver Manager]DRIVER keyword syntax error 2: In
RODBC::odbcDriverConnect("DSN=DSN=/usr/share/cassandra/lib/cdata.jdbc.cassandra.jar")
:
ODBC connection failed
And my code is there:
library(RODBC)
con <-odbcConnect("DSN=/usr/share/cassandra/lib/cdata.jdbc.cassandra.jar")
It looks like the problem is that you're trying to use the CData JDBC Driver with RODBC. We produce separate JDBC and ODBC drivers for Cassandra, but either will allow you access your Cassandra data in R.
You can review this article for connecting to Cassandra in R using our ODBC driver, though I've included the relevant steps below:
Configure the DSN
If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.
Set the Server, Port, and Database connection properties to connect to Cassandra. Additionally, to use internal authentication set the User and Password connection properties.
Load the RODBC Package
library(RODBC)
Create a connection to your Cassandra data, using the name of the DSN you configured above
conn <- odbcConnect("CData Cassandra Source")
Once you've established the connection to your Cassandra data, you can perform all manner of operations in R, including:
Schema discovery
sqlTables(conn)
Executing SQL queries and view the results
customer <- sqlQuery(conn, "SELECT City, SUM(TotalDue) FROM Customer GROUP BY City", believeNRows=FALSE, rows_at_time=1)
View(customer)
Plot Cassandra data
par(las=2,ps=10,mar=c(5,15,4,2))
barplot(customer$TotalDue, main="Cassandra Customer", names.arg = customer$City, horiz=TRUE)

Resources