Connect to teradata using DBI Package in R - r

Can you anyone help me with how to connect to Teradata using DBI ODBC Package?
I use the code below,
con <- dbConnect( drv = dbDriver('Teradata'),
server=prodServer,
DBCName=prodDatabaseName,
uid=username,
pwd=password,
MechanismName = TD2)
but it throws the following error:
Error: Couldn't find driver Teradata. Looked in:
* global namespace
* in package called Teradata
* in package called RTeradata

Looks like Teradata ODBC driver, DBCName is a network name (which for other drivers would typically supplied as Server)
con <- dbConnect(odbc::odbc(),
Driver = DRIVER,
DBCName = SERVER,
Database = defaultDatabase,
UID = Sys.getenv("tera_user"),
PWD = Sys.getenv("tera_pass"))
After quite a looot of research, I finally found this in an answer by #Fred to another question:
R-Studio - connection to Teradata is not working

Related

Unknown error when trying to connect to postgresql from R with RPostgresql [duplicate]

I'm new to R and I'm trying to connect to PostgreSQL using RStudio.
I've installed the RPostgreSQL and tried the following code:
> library("DBI", lib.loc="~/R/win-library/3.2")
> library("RPostgreSQL", lib.loc="~/R/win-library/3.2")
> con <- dbConnect(dbDriver("PostgreSQL"), dbname="Delta", user="postgres")
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres#local on dbname "Delta"
I'm not able to connect to the database for some reason. I'm trying to solve this issue for a long time and couldn't figure out how.
My solution to this problem is to use RPostgres https://github.com/rstats-db/RPostgres.
Assuming you have a connection url, the following code will work:
library(DBI)
library(RPostgres)
con <- dbConnect(RPostgres::Postgres(),
host = url$host,
port = url$port,
dbname = url$dbname,
user = url$user,
password = url$password
)
My solution using the odbc package
db <- DBI::dbConnect(odbc::odbc(),
Driver = "{PostgreSQL ODBC Driver(ANSI)}",
Database = "db_name",
UserName = "user",
Password = "pass",
Servername = "localhost",
Port = 5432)
Running into this unclear error, I found RPostgreSQL will work by adjusting the PostgreSQL password encryption from the default of libpq 10 at scram-sha-256 to md5. See this SO post: How can I solve Postgresql SCRAM authentication problem?
I arrived at this fix by using an ODBC driver connection, specifically replacing DBI + RPostgreSQL packages for DBI + odbc which raised a much clearer error:
SCRAM authentication requires libpq version 10 or above.
Changing the authentication worked for both odbc and RPostgreSQL connections in R.
Do note: the user for R-PostgreSQL connection password must be adjusted (even with the same exact one) since the encrypted authentication will be changed:
-- SUPERUSER
ALTER USER postgres WITH PASSWORD 'new or same password';
-- LOGIN USER
ALTER USER myuser WITH PASSWORD 'new or same password';
I had the same problem . I installed DBI after I installed RPostgreSQL and loaded the DBI library seperately. Worked for me

Unable to query Azure SQL Server from RStudio

I am trying to query an SQL Server database deployed on Azure from RStudio.
I established a connection. I can see all of the tables and get a preview of the data from the RStudio GUI. However, when I try to query the data I get an error.
library(dplyr)
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "#.database.windows.net",
Database = "loremipsum",
UID = "loremipsum",
PWD = "loremipsum",
Port = 1433)
First query I tried:
users <- as.data.frame(sqlQuery(con, "SELECT * FROM AspNetUsers"))
The error I got:
Error in sqlQuery(con, "SELECT * FROM AspNetUsers") :
first argument is not an open RODBC channel
I also tried this query
result <- dbSendQuery(con, "SELECT * FROM AspNetUsers")
first_100 <- dbFetch(result, n = 100)
With the following error:
Error in result_fetch(res#ptr, n) :
nanodbc/nanodbc.cpp:2966: 07009: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
It strange because the connection must be valid, given that it gets access to the data through the GUI.
Any hints?
Congratulations you have solved the issue:
I eventually solved the issue by switching to another libary - RODBC. It seems that thats a bug in the way odbc handles character columns as point:R DBI ODBC error: nanodbc/nanodbc.cpp:3110: 07009: [Microsoft][ODBC Driver 13 for SQL Server]Invalid Descriptor Index
I post this as answer and this can be beneficial to other community members.

Connect to MSSQL using DBI

I can not connect to MSSQL using DBI package.
I am trying the way shown in package itself
m <- dbDriver("RODBC") # error
Error: could not find function "RODBC"
# open the connection using user, passsword, etc., as
# specified in the file \file{\$HOME/.my.cnf}
con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))
Any help appreciated. Thanks
As an update to this question: RStudio have since created the odbc package (or GitHub version here) that handles ODBC connections to a number of databases through DBI. For SQL Server you use:
con <- DBI::dbConnect(odbc::odbc(),
driver = "SQL Server",
server = <serverURL>,
database = <databasename>,
uid = <username>,
pwd = <passwd>)
You can also set a dsn or supply a connection string.
It looks like there used to be a RODBC driver for DBI, but not any more:
http://cran.r-project.org/src/contrib/Archive/DBI.RODBC/
A bit of tweaking has got this to install in a version 3 R but I don't have any ODBC sources to test it on. But m = dbDriver("RODBC") doesn't error.
> m = dbDriver("RODBC")
> m
<ODBCDriver:(29781)>
>
Suggest you ask on the R-sig-db mailing list to maybe find out what happened to this code and/or the author...
Solved.
I used library RODBC. It has great functionality to connect sql and run sql queries in R.
Loading Library:
library(RODBC)
# dbDriver is connection string with userID, database name, password etc.
dbhandle <- odbcDriverConnect(dbDriver)
Running Sql query
sqlQuery(channel=dbhandle, query)
Thats It.

connecting form R (client) to Greenplum server

I'm trying to retrieve data from greenplum cluster into R (win client).
I've tried:
library("RODBC")
conn <- odbcDriverConnect("DSN_name")
Sql <- "select * from DB.st.country"
cen_data <- sqlQuery(conn,Sql)
print(cen_data)
I'm getting error:
0A000 7 ERROR: cross-database references are not implemented
I have seen some answers about dblink but when I tried:
sql <- "select dblink_connect('conn', 'dbname=myDB');"
cen_data <- sqlQuery(conn,Sql)
I'm getting error:
"42883 7 ERROR: function dblink_connect(unknown, unknown) does not exist
Does anyone have any idea what Am I doing wrong?
Instead of ODBC, you can also use the RPostgreSQL package, which uses DBI as the backend.
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="hostname", user=..., pass=...)
This is not an R issue (syntax is ok).
The problem was in database definitions.
You need to have the database in the "select data source". for that you need to have postgreSQL.

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