DB2 ODBC connection doesn't work on R 4.2 - r

I had a working connection to a DB2 server from R. Then I ungraded to R v4.2 and it no longer works.
This is my connection string:
con_DB2 = DBI::dbConnect(odbc::odbc(),
Driver = "IBM DB2 ODBC DRIVER - C_PROGRA~2_IBM_V111~1.4FP_CLIDRI~1",
Database='DB2Q',
Hostname='usddcs',
Port=3700,
PROTOCOL='TCPIP',
UID= rstudioapi::askForPassword("Database username"),
PWD=rstudioapi::askForPassword("Database password"))
I get the following error message:
Error: nanodbc/nanodbc.cpp:1021: IM004: [Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed
This seems to have been raised in this issue: https://github.com/rstudio/rstudio/issues/10509
It was NOT solved, but suggestions are that the encoding might be impacting this. Are there any arguments to the dbConnect() function that can be changed to fiddle with encoding to make it work in the new R version?
> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Works for me with R4.2.2 on Win10 pro(19045) and Rstudio 2022.07.02-576 and
IBM clidriver version 11.5.6.0 (or higher). It may also work with older driver versions.
The key detail is that you need to have the system environment variable DB2CODEPAGE=1208 set before starting Rstudio. This tells the clidriver to use utf-8 as the application code page.
If you only want the change to impact Rstudio and R (but not separate command-line based programs, for perl, python, php, clp etc ) then set that variable in the Rstudio script via Sys.setenv(DB2CODEPAGE=1208) or equivalent configuration/startup file.
Note that if you are not using clidriver, and instead you are using a larger footprint Db2 client (for example, the fat client, or the runtime client) then you may also use the db2set DB2CODEPAGE=1208 method of setting the variable, and the consequence should be the same.
Then the database connection to Db2-LUW succeeds (the target database is also utf-8 encoded).
You can also ignore the cli driver and instead try accessing the database via rJDBC and a jdbc driver (there is no shared code between the jdbc driver and the cli driver).

Related

Connecting Oracle to R issue

I am trying to connect Oracle to RStudio using the "ROracle" package. I've installed the package and loaded it. I also have the "DBI" package installed and loaded.
I am using dbConnect(dbDriver("Oracledrivername"),oracle_schema,oracle_password,dbname="dbname") to connect to my oracle schema but I am getting this error:
Error in h(simpleError(msg,call)):
eror in evaluating the argument 'drv' in selecting a method for function 'dbConnect'
I then tried to narrow it down by testing dbDriver("Oracledrivername") by itself and the Error I get is:
Error: Couldn't find driver Oracledrivername
Things that I have done to attempt to fix this are:
I tested my connection to "Oracledrivername" in the ODBC data source administrator, the connection was ok.
The Rstudio I am using is 64 bit, Oracle client is a v12.1.0 64 bit, and the ODBC driver was set up on 64 bit
I have set the oracle_home location to C:\ORACLE12_64BIT\product\12.1.0\client_1

Connect to HSQLDB 2.5 via Windows ODBC PostgreSQL driver

I am having a problem configuring the Windows PostgreSQL ODBC driver to connect to HSQLDB 2.5.0. As per the HSQLDB documentation I have installed version 11.01 of the PostgreSQL ODBC driver. When I test the connection from the ODBC Data Source Administrator I see the following in the ODBC log file: [0.000]Driver Version='11.01.0000,May 24 2019' linking 1915 dynamic Multithread library
[0.000]PQconnectdbParams: host='localhost' port='9001' dbname='test' user='test' sslmode='disable' password='test'
[0.109]PQsendQuery: 000000000033BCA0 'SET DateStyle = 'ISO';SET extra_float_digits = 2;show transaction_isolation'
[0.109] (ERROR) 42501 'user lacks privilege or object not found: DATESTYLE'
[1.157]PQfinish: 000000000033BCA0
It looks like the driver is sending a "SET DateStyle" command that HSQLDB doesn't understand. I've tried changing all the datasource options with no success. I have tried both the Unicode and ANSI versions of the driver.
The documentation on the web site is for version 2.5.1 which is in Release Candidate stage. You can download a snapshot jar from http://hsqldb.org/download/

Missing letters of database objects being returned in DBI SQL Server ODBC connection

Unfortunately, I will not be able to create a good repro for this issue without sharing confidential creds to the database I am having issues with. Hopefully I have enough information below to flag any obvious problems that ODBC experts will understand.
Background
I am running a MacBook Pro with the following specs:
Model Name: MacBook Pro
Model Identifier: MacBookPro15,1
Processor Name: 6-Core Intel Core i7
Processor Speed: 2.6 GHz
Number of Processors: 1
Total Number of Cores: 6
L2 Cache (per Core): 256 KB
L3 Cache: 9 MB
Hyper-Threading Technology: Enabled
Memory: 32 GB
Boot ROM Version: 1037.0.78.0.0 (iBridge: 17.16.10572.0.0,0)
My ODBC connection is set using FreeTDS as specified here.
The relevant portion of freetds.conf is as follows:
# The POC SQL Server
[POC]
host = 172.22.238.154
port = 1433
tds version = 7.3
My odbcinst.ini file is as follows:
[FreeTDS]
Description=FreeTDS Driver for Linux & SQL Server
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
My odbc.ini file is specified as follows:
[POC]
Description = Connecton to Partners for our children SQL Server
Driver = FreeTDS
Servername = POC
I am trying to make a connection to a SQL Server 2012 database (via VPN) using the following connection information in R:
con <- DBI::dbConnect(odbc::odbc()
,dsn = "POC"
,uid = Sys.getenv("MSSQL_UN")
,database = "CA_ODS"
,pwd = Sys.getenv("MSSQL_PW"))
This generates the following connection object:
> con
<OdbcConnection> POC2
Database: CA_ODS
Microsoft SQL Server Version: 11.00.7001
In general, this connection works as expected. I can query the database using DBI::dbGetQuery(con, "select * from MyTable"), dplyr::tbl(con, MyTable), etc. without issue.
Problem
RStudio, however, is only displaying every other letter of the database objects, and truncating the object names after the first several letters. The following screenshot should illustrate the issue well:
The database I am trying to connect to is called CA_ODS. However, the RStudio object browser is only displaying every other letter of the database name (i.e. the DB is listed as C_D).
This does not appear to be limited to RStudio per se either. While the results of the actual database queries work fine as described above, the returned names from the INFORMATION_SCHEMA appear to match the information in the object browser. Below, when run directly from SQL Server Management Studio, the returned TABLE_CATALOG is CA_ODS, TABLE_SCHEMA is ndacan, etc. When run via the DB connection, however, I get the following.
> DBI::dbGetQuery(con, "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_SCHEMA='ndacan'")
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
1 C_D naa f21v BASE TABLE
Question
Any suggestions as to how I can respecify my ODBC connection in R or in my FreeTDS configs to get the full name of database objects returned?
As noted in #r2evans comments, this appears to be an issue with odbc, running in R 3.6.0, on a Mac.
In general, it appears that this can be fixed by reinstalling odbc from source install.packages("odbc", type = 'source').
As also noted in the comments, I had recently upgraded my Mac to Catalina. Prior to installing odbc from source I needed to first reinstall XCode using xcode-select --install from my terminal.
As can be seen in the screen capture below, I am now getting the full object names displayed from the odbc connection.

ROracle Error in .oci.Driver

I'm trying to use ROracle. My Oracle database version is Oracle Database 12c Release 12.1.0.1.0 - 64bit Production, and I have installed Instant Client and SDK for the version 12.1.0.2.0 (12.1.0.1.0 isn't available on Oracle's site).
I can access the Oracle database through Oracle SQL Developer without any issues. The error message I get in RStudio is:
Error in .oci.Driver(.oci.drv(), interruptible = interruptible, unicode_as_utf8 = unicode_as_utf8, :
In case it's useful, I have installed Instant Client and SDK here:
C:\oreclient_install_dir\instantclient_12_1
I'm running a Windows machine and I followed the instructions on CRAN:
http://cran.us.r-project.org/web/packages/ROracle/INSTALL
Including adding the environment variables.
After some searching (e.g. here) it looks like I need to set LD_LIBRARY_PATH and ORACLE_HOME and "OCI_LIB", which I did:
Sys.setenv(LD_LIBRARY_PATH="C:/oreclient_install_dir/instantclient_12_1")
Sys.setenv("ORACLE_HOME" = "C:/oreclient_install_dir/instantclient_12_1")
Sys.setenv("OCI_LIB" = "C:/oreclient_install_dir/instantclient_12_1")
Same error comes up. I'm likely not doing this right, however; I'm not a DBA and don't know all the details.
Any help with this would be appreciated.
LD_LIBRARY_PATH is for Linux. Assuming that you already have Rtools installed in C:\Rtools, you can use the following code to install ROracle
Sys.setenv(OCI_LIB64="C:\\oreclient_install_dir\\instantclient_12_1")
Sys.setenv(PATH=paste0(Sys.getenv("PATH"),";C:\\oreclient_install_dir\\instantclient_12_1"))
install.packages("ROracle", type="source", INSTALL_opts="--no-multiarch")
For using the ROracle package, you can use the following:
Sys.setenv(OCI_LIB64="C:\\oreclient_install_dir\\instantclient_12_1")
Sys.setenv(PATH=paste0(Sys.getenv("PATH"),";C:\\oreclient_install_dir\\instantclient_12_1"))
library(ROracle)

Reading a access database (mdb) in 64 bit in R

I have a database and I need to read that in R. I found some packages such as Hmisc and RODBC which have the functions to do that. I am using windows and was not able to use Hmisc because you need to have mdb-tools package and I found no tutorial or way to install mdb-tools on windows.
Now, I was trying to start with RODBC. I found this question "How to connect R with Access database in 64-bit Window?" which shows how to have a connection in windows. I tried to use the command similar to what was accepted answer in that question.
odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb")
It gives the following error :
1: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}, DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb") :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC Driver Manager] Invalid connection string attribute
2: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}, DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb") :
ODBC connection failed
I am not sure how to check and start diagnosing what's going on here. I went to administrative tools and checked the options on "Data Sources (ODBC)". . I changed the target to sysWOW.
Then I created a new data source as follows:
I am not sure if I need to select database or not. I found Brian Ripley's http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf RODBC tutorial but still I am not able to make it work.
This works fine for me & might work for you, too:
require(RODBC)
conn <- odbcConnectAccess2007(path.expand("~/Database.accdb"))
subset(sqlTables(conn), TABLE_TYPE == "TABLE")
df <- sqlFetch(conn, "Table1")
close(conn)
My sessionInfo():
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
#
# other attached packages:
# [1] RODBC_1.3-10
#
# loaded via a namespace (and not attached):
# [1] tools_3.1.1
I have had issues with this (trying to query Access 32bit from R 64bit) from a long time. I think it has been fixed in windows 10.
I made a kludge by modifying something I found in this post:
How to connect R with Access database in 64-bit Window?
I made a function that saves a script (which in turn connects to the database and saves the result of the query), run it using R32, and load the data into the R64 work environment.
I prepared it for Access 2007, but something analogue could be done for Access2003 (just using odbcConnectAccess instead of odbcConnectAccess2007) or other 32 bit databases
MysqlQueryAccess2007<-function(filename,query){
tempdir=gsub('\\\\','/',tempdir())
txt<-paste("if (!'RODBC' %in% installed.packages()) install.packages('RODBC')
require(RODBC)
channel<-odbcConnectAccess2007('",filename,"')
data<-sqlQuery(channel,\"",query,"\")
save(data,file=paste('",tempdir,"','tempRODBCquery.Rdata',sep='/'))
close(channel)",sep="")
writeLines(txt,con=paste(tempdir,'RODBCscripttemp.r',sep='/')->tempscript)
system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ",tempscript))
tt<-get(load(paste(tempdir,'tempRODBCquery.Rdata',sep='/')))
return(tt)
}
Then you only have to do the queries this way:
dat<-MysqlQueryAccess2007("samplefile.accdb","SELECT TOP 5 * FROM TableI")
Have been trying to figure it out for a while myself.
Solution given in the accepted answer here
Reading data from 32-bit Access db using 64-bit R, credits to #erg,
as well as here
How to connect R with Access database in 64-bit Window?, credits to #JATT.
The bottom line:
Install 64-bit Microsoft Access drivers https://www.microsoft.com/en-us/download/details.aspx?id=54920
Setup appropriate System DSN in ODBC Data Sources (64-bit)
In R 64-bit read .mdb file by using odbc package: dbConnect(odbc(), 'your_64bit_dsn').

Resources