How do I connect to an SQL Server d atabase using R and Rstudio? (Mac) - r

Disclaimer: I'm a statistician/bioinformatician by training, so I'm quite new to networks, servers, and databases.
System: Macbook Pro (M1 chip).
I'm trying to connect to an SQL Server database remotely via R and RStudio.
To start, I ran the following commands in terminal (as seen here https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver16):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18
The code I'm running in RStudio is as follows (as seen here https://db.rstudio.com/getting-started/connect-to-database):
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 18 for SQL Server",
Server = "xxx.xxx.xxx.xx",
Database = "Dbname",
UID = "username",
PWD = "password",
Port = 3306,
.connection_string ="TrustServerCertificate=yes")
The above gives me the following error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Data
source name not found and no default driver specified
I can't find any help related to the errors I'm getting at https://db.rstudio.com/getting-started.
Slightly different piece of code gives me a different error:
con <- DBI::dbConnect(odbc::odbc(),
.connection_string = "Driver={ODBC Driver 18 for SQL
Server};Uid=username;Pwd=password;Host=xxx.xxx.xxx.xx;Port=3306;Database=Dbname;TrustServerCertificate=yes;")
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 18 for
SQL Server]Neither DSN nor SERVER keyword supplied [Microsoft][ODBC
Driver 18 for SQL Server]Invalid connection string attribute
What is a Server Keyword as referred to in the second error? Is the server supposed to be an IP address as I've indicated in the code?
Does the use of ODBC Driver matter? How can I tell if I'm using the right one?
Am I off the mark with any of the information I'm feeding into dbConnect()?
Any tips welcome.
Thanks.

Thanks to #AlwaysLearning for pointing at the mistake in my second chunk of code. Host= should have been Server=.
As for the first chunk, I don't know why it wouldn't work.

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.

Trouble accessing oracle database using R (odbc package)

A colleague at work is having trouble using the odbc package function. I am trying to find help.
He is using an oracle database using R instead of running our traditional SAS programs, but he has not been successful. We are trying to find out what is causing the error messages below. Can someone help?
Attempt 1:
#Get the Oracle JDBC driver
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:/instantclient_19_10/ojdbc8.jar")
Create connection string to the Database we want
connect.string <-
glue("jdbc:oracle:thin:#//{host}:{port}/{sid}",
host = "stdbprd01.states.bls.gov",
port = 1521,
sid = "lausonep")
print(connect.string)
#Establish connection to your database
con <- dbConnect(jdbcDriver,
connect.string,
user = "username",
password = rstudioapi::askForPassword("Database password"))
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Attempt 2:
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
driver="Oracle in OraClient12Home1",
database="lausprd",
uid="aakre_n",
pwd="!QAZ1qaz#WSX2wsx",
host="stdbprd01.states.bls.gov",
port=1521
)
Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][ORA]-12560: TNS:protocol adapter error
[Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed

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")

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

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

"[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN

Getting the "[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN referenced in the connection string. Why would this be happening and what can be done?
Trying to set up pyodbc for connecting to MSSQLServer using the docs (centos 7). Yet, when trying to actually connect to the database,
import pyodbc
# setup db connection
server = 'myserver'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn_str = 'DSN=MyMSSQLServer;DATABASE='+database+'UID='+username+'PWD='+password+'MultipleActiveResultSets=True;'
cnxn = pyodbc.connect(cnxn_str)
getting error "[unixODBC][Driver Manager]Data source name not found, and no default driver specified", even though when running:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer] Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=<now using my sql server ip>
I can see that the DSN that I referenced in the python code is recorded in the /etc/odbc.ini file. Does anyone know what could be going on here? Thanks.
Note: I initially ran the pyodbc setup exactly as specified in the docs, but later re-did this step to use the ip address of the sql server I wanted to connect to:
vi /home/user/odbcadd.txt
[MyMSSQLServer] Driver = ODBC Driver 13 for SQL Server
Description = My MS SQL Server
Trace = No
Server = <my sql server ip>
and just re-ran the write to odbc.ini:
sudo odbcinst -i -s -f /home/user/odbcadd.txt -l
The solution commented by user Gord Thompson solved my problem. The ~/odbcadd.txt file that I was using to fill the /etc/odbc.ini file needed to have the 'Driver=' line on a separate line from the DSN to look like:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer]
Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=172.18.4.38

Resources