RODBC odbcDriverConnect() Connection Error in shiny R but not in Rstudio - r

I'm trying to use
odbcDriverConnect("driver={SQL
Server};server=IP;database=DBNAME;uid=username;pwd=password")
to connecting sql server database from shiny R,
but its result is -1.
I run the same code in R studio and get results normally, what's the problem?
For testing more I tried this one:
con <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "IP",
Database = "dbname",
UID = "username",
PWD ="password",
Port = Port)
But again it runs in R studio but not in shiny and gets this error:
nanodbc/nanodbc.cpp:950: IM002: [unixODBC][Driver Manager]Data source name
not found, and no default driver specified

Related

Trying to work on R using Azure ML Studio Notebook and facing challenges with ODBC package

I am trying to work on R notebook on ML Studio. Using regular python is easy and works as expected but with R i am facing challenges.
While trying to connect to MS SQL database using odbc() :
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server",
Database = "db",
UID = "user",
PWD = "password",
Port = 1433)
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
As suggested in some posts, i have also tried replacing Driver = "SQL Server", with Driver = "ODBC Driver 11 for SQL Server". But i see similar error
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 11 for SQL Server' : file not found
Traceback:
Please suggest a work around.
We can assign the Driver as the version of the SQL server and get the password using the API call.
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 13 for SQL Server",
Server = "your server IP address",
Database = "Database name",
UID = "User ID",
PWD = rstudioapi::askForPassword("password"),
Port = port_number_under_user)
In some cases, Driver = "SQL Server” will also work fine. If not functioning. Use the above code block.

ODBC Connection to ORACLE in R

I am using both the DBI and ODBC package in "R" in order to make a connection to an ORACLE database.
Here is the connection code I am using:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ORACLE",
Host = "orasada.ca",
SVC = "STG",
UID = "username",
PWD = "password",
Port = 1521)
Everything looks fine to me, but I keep getting this error:
Error: nanodbc/nanodbc.cpp:983: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any ideas on what I can change to make a successful connection?
Thanks.

MongoDB Connection in Rstudio with odbc

I'm trying to connect to a mongoDB database using library(odbc) in R. First I installed the driver from here and then I have used the following method:
con <- dbConnect(odbc::odbc(),
Driver = "MongoDB ODBC 1.3.0 Unicode Driver",
Server = "xxxxx",
AuthMechanism = "SCRAM-SHA-1",
Port = 27017,
Database = "test",
UID = "utest",
PWD = "ptest"
)
However the following error will happen:
Error: nanodbc/nanodbc.cpp:983: 08S01: [MySQL][ODBC 1.3(w) Driver]Lost
connection to MySQL server at 'waiting for initial communication
packet', system error: 10060
I would appreciate any help.Thanks

Unable to connect to ODBC through shinyapps.io

I build a r shiny app which uses the odbc library to fetch the data from server hosted in public server and shows the result output. It is working perfectly under Win10.
I am using the below code to connect to server:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "****",
Database = "****",
UID = "****",
PWD = "****")
When I hosted the app in www.shinyapps.io it is not working. It shows the error as below:
Warning: Error in : nanodbc/nanodbc.cpp:950: 01000: [unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 11.0' : file not found
I am unable to why it says 'file not found'? The app works perfectly when I run the app using rstudio from my PC.
Please help.
Thanks in advance.
Sumanta
You have to use FreeTDS. I found either using 7.4 or 7.0 version.
try this using odbc:
library(DBI)
library(odbc)
con <- dbConnect(
odbc(),
Driver = "FreeTDS",
Database = database,
Uid = uid,
Pwd = pwd,
Server = server,
Port = 1433,
TDS_Version = 7.4
)
or this usind rodbc:
library(RODBC)
con <- odbcDriverConnect(
'Driver=FreeTDS;
TDS_Version=7.4;
Server=<server>;
Port=<port>;
Database=<db>;
Uid=<uid>;
Pwd=<pw>;
Encrypt=yes;
TrustServerCertificate=no;
Connection Timeout=30;')

Establish Microsoft SQL Server Connection with R/RStudio

I am trying to connect R with Microsoft SQL server. I have used Toad for SQL Server 6.8 so far for my queries. However, for some other analysis (which can be easily performed in R) I want to connect database with R.
I have tried R function "dbconnect" with providing server name and database name. See query below:
odbc_con <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "xxxxx",
Database = "yyyyy",
Uid = 'US\dhrdesai',
Pwd = rstudioapi::askForPassword("Database password"),
Port = 1433)
However, I got following errors:
Error: nanodbc/nanodbc.cpp:950: IM002: [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified
and
Error: unexpected ')' in " Port = 1433)"
Have anyone faced the same or know any other way to connect R with SQL server.
You need to use a double backslash \\ every time you see a \. I just made my connection work yesterday with the following code. Also pehaps you have not installed all the packages that is required.
library(DBI)
library(dbplyr)
library(odbc)
con <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "path\\path", # remember \\ if your path has a \
Database = "the_database_name",
user = "your_user_name", # remember \\ if your username has a \
Trusted_Connection = "True")

Resources