Reading a access database (mdb) in 64 bit in R - 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').

Related

DB2 ODBC connection doesn't work on R 4.2

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

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

how to read data from Cassandra (DBeaver) to R

I am using Cassandra CQL- system in DBeaver database tool. I want to connect this cassandra to R to read data. Unfortunately the connection takes more time (i waited for more than 2 hours) with RCassandra package. but it does not seem to get connected at all and still loading. Does anyone has any idea on this?
the code as follows:
library(RCassandra)
rc <- RC.connect(host ="********", port = 9042)
RC.login(rc, username = "*****", password = "******")
after this step RC.login, it is still loading for more than 2 hours.
I have also tried using RJDBC package like posted here : How to read data from Cassandra with R?.
library(RJDBC)
drv <- JDBC("org.apache.cassandra.cql.jdbc.CassandraDriver",
list.files("C:/Program Files/DBeaver/jre/lib",
pattern="jar$",full.names=T))
But this throws error
Error in .jfindClass(as.character(driverClass)[1]) : class not found
None of the answers are working for me from the above link.I am using latest R version 3.4.0 (2017-04-21) and New version of DBeaver : 4.0.4.
For your first approach, which I am less familiar with, should you not have a line that sets the use of the connection?
such as:
library(RCassandra)
c <- RC.connect(host ="52.0.15.195", port = 9042)
RC.login(c, username = "*****", password = "******")
RC.use(c, "some_db")
Did you check logs that you are not getting some silent error while connecting?
For your second approach, your R program is not seeing a driver in a classpath for Java (JMV).
See this entry for help how to fix it.

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.

dbConnect with R 3.0 on Ubuntu 12.04 x64 --Error in as.integer(from) : cannot coerce type 'S4' to vector of type 'integer'

Just updated to R 3.0 and updated all the packages, including DBI. To my surprise, a script that I often use stopped working.
I am unable to connect to a MySQL database using dbConnect. The code script instantly, so only a few lines will reproduce the problem
> require("RMySQL")
> m = dbDriver("MySQL")
> dbConnect(m, user = 'user', password = 'pass', dbname = 'dbname', host = 'localhost', client.flag = CLIENT_MULTI_STATEMENTS)
Error in as.integer(from) :
cannot coerce type 'S4' to vector of type 'integer'
Calls: dbConnect ... mysqlNewConnection -> isIdCurrent -> as -> asMethod
Also tried it as:
dbConnect(MySQL(), user = 'user', password = 'pass', dbname = 'dbname', host = 'localhost', client.flag = CLIENT_MULTI_STATEMENTS)
but the same problem
Also tried removing other parameters, but the same issue from the dbDriver.
What changed in the DBI package with the latest update? How can I fix this?
I noticed that the DBI package is orphaned so don't know who to ask.
I had the same issue with R 3.0.1 on ubuntu.
Installing the latest version of the RMySQL-package resolved the problem:
> install.pacakges("RMySQL")
Make sure to restart R after the installation.
I'm still digging into the issue, but I think I've identified multiple causes of this issue. At their root, they all have to do with R expecting an S4 object but getting back an integer instead. I believe these are generally a result of the connection failing to establish.
Why is it failing? One thing I've noticed is that if you fail to close to many of your connections (~16 [see the number of maximum connections specified in the driver handle call] open) DBI won't/can't open a new connection. Make sure you are calling dbDisconnect as needed. Usually, this sort of problem results in a sensible error message, however sometimes results in the above referenced error. If possible access the DB through an abstraction layer, e.g. dplyr as some will monitor the db connections and kill them if they are inactive. Whereas, AFIK if you open a connection in a function and the function breaks, you have no way to close the open connection unless you returned the driver object from your initial call to dbConnect. In this case you have no choice but to restart your instance of R (possibly resetting your machine and clearing your workspace as well).
The other issue I recently encountered is that if RMySQL masks RPostgreSQL, then RPostgreSQL will fail. The reverse does not appear to be the case, but because others have mentioned RPostgreSQL in here as showing the same error message, it seemed worthy of note.
Update
The latest version of RMySQL (0.10.1) seems to have finished off RPostgreSQL - RPostgreSQL now fails to work regardless of load order. The same people working on RMySQL appear to be working on RPostgres (https://github.com/rstats-db/RPostgres) and this conflict appears to be a non-issue if using that package instead of RPostgreSQL. Specifically, use RPostgres::Postgres() in the place of RPostgreSQL::PostgreSQL() when specifying the driver in dbConnect. Other packages, e.g. dplyr, currently assume RPostgreSQL, so this issue can still bite (but it seems a resolution is in the works (https://github.com/rstats-db/RMySQL/issues/28).

Resources