R SQL Server ODBC connection Escape Character \ in Server Name - r

I am trying to connect my R Studio session to my SQL Server database using library(odbc).
However, the server name is something like "xxx\xxxx", and R is complaining about "an unrecognized escape in character string starting ""xxx\xxxx".
After some googling, I tried Server = ... with two \s then it just complains about "Data source name not found and no default driver specified".
Any insight is appreciated.
Thanks.
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQLServer",
Server = "xx\xxxx",
Database = "xx",
UID = "axx",
PWD = "axx",
Port = 1xxx)
SQL Server Enterprise Edition 64-bit Version 10.50.2500.0;
RStudio Version 1.1.423

I got it resolved, after installing ODBC driver on the PC for SQL Server
#LIBRARY#
library(DBI)
library(shiny)
library(dplyr)
#CONNECTION#
con <- dbConnect(odbc::odbc(), "xxxx")
Thanks

Related

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

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.

Connection R with odbc using ini -fle

I am trying to connect to a MySQL database from within R using odbc. If I write the connection information it works well, however, if I put the same information in a odbc.ini file, I get an error.
Here is the code that works
library(DBI)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "localhost",
UID = "myname",
PWD = "mypassword",
Database = "vgr",
encoding = "latin1",
Port = 3306)
And here the code that doesn't work
con <- DBI::dbConnect(odbc::odbc(), 'MySQL')
The error is (it looks like dbConnect doesn't find the information):
Error: nanodbc/nanodbc.cpp:983: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
This is in my odbc.ini either in the working directory or where Windows saves it ODBC.ini file.
[MySQL]
Driver = MySQL ODBC 8.0 ANSI Driver
Server = localhost
UID = myname
PWD = mypassword
Database = vgr
encoding = latin1
Port = 3306
Any help would be appreciated.
Cheers
Renger

Connecting to Impala from R without Impala JDBC driver

For an undisclosed reason, my Impala does not have a JDBC driver installed. This is making the connection from R to Impala challenging.
I am able to connect (and query) to Impala shell via Putty. E.g.,
impala-shell --ssl -i some_name
Using the Putty connection mechanism/credentials, can this be performed from RStudio and bring in the SELECT results into a dataframe?
This worked in my Oracle BDA cluster.
library(dsreq)
print("Connecting to Impala...")
impaladb <- impalaConnection(pool='general')
dbResultsTempTbl <- dbGetQuery(impaladb, paste0("SELECT * FROM mytable") )
print("results")
print(dbResultsTempTbl)
You can use the ODBC driver to connect to impalaDB
library(ODBC)
drv <- odbc::odbc()
con <- DBI::dbConnect(drv = drv, driver = "Cloudera ODBC Driver for Impala",
host = "your hostname", port = 21050, Schema = "your schema")

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