odbcConnect with Access database difference between R and RStudio - ms-access

I can use the following code successfully in the R GUI to connect to a Microsoft Access database:
library(RODBC)
#ODBC connection
scallopchannel<-odbcDriverConnect("scallop_2016")
"scallop_2016" is a DSN for the Access database I connect to.
In RStudio I have to specify to path to the Access database like this:
ODBC connection
scallopchannel<-odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=S://Adv/Scallop Central/2015 RSA/2015 RSA Database/2015 RSA Database_9-4-2015.accdb")
Otherwise I get an error message:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=db2017") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In RODBC::odbcDriverConnect("DSN=db2017") : ODBC connection failed
When I type to troubleshoot the problem
odbcDataSources ()
RStudio indicates that the Access datasource is there.
This is a pain since I sometimes connect to multiple databases within a session and I prefer to be able to code for just the DSN.
I am using RStudio 1.0.153 and R GUI 3.3.2.
I am also using 32 bit R and 32 bit RStudio.

Related

Reading an .mdb file into R

Using Windows 10 Enterprise and R 4.1.2, I am trying to read MS Access .mdb files into R. It seems the RODBC package is the best (only?) choice available.
I've set the R version to the 32-bit version and tried variations on the following:
library(RODBC)
ch <- RODBC::odbcConnectAccess("C:/live.mdb")
In all cases, I get
Warning messages:
In odbcDriverConnect(con, ...) : [RODBC] ERROR:
state HY000, code -1028, message [Microsoft][ODBC Microsoft Access
Driver] Cannot open database '(unknown)'. It may not be a database
that your application recognizes, or the file may be corrupt.
In odbcDriverConnect(con, ...) : ODBC connection failed
I can open and browse the MS Access database in Access 2016 without issue.
I swear I tried this earlier, but it didn't work. I guess I fat-fingered it. Nonetheless, this option is working for me in the 64-bit version.
ch <- odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/live.mdb")

Can't use ODBC connection to Access from .Net Core 3.0

I'm trying to connect to an Access .accdb file and read data from my c# project in VS 2019 (.Net Core 3.0). I'm on a 64-bit system, and I believe I have both 32- and 64-bit odbc drivers for Access installed.
I was able to setup a Data connection in server explorer which uses a data source name of "MS Access Database". When I test the connection, it works fine. Also, I can expand the new connection in server explorer, and I can see all of the tables just fine. I right-click the data connection and choose Modify, I can see the data source name is MS Access Database, and if I select "use connection string", the full connection string is
Dsn=MS Access Database;dbq=C:\Brandeis\BNCConejo_Data.accdb;defaultdir=C:\Brandeis;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin.
As mentioned, when I use either the dsn or the full connection string in the modify connection dialog, clicking the "Test Connection" button gives me a success message. If I use either the DSN or full connection string in my code, I get this error:
ERROR [IM014] [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
Here is a snippet of my code:
using (var conn = new OdbcConnection(connectionString))
{
try
{
conn.Open();
}
catch (OdbcException ex)
{
LastError = "ODBC Error: " + ex.Message;
return null;
}
}
I've tried targeting Any CPU, x64 and x86, but none of them work. They all return the above exception message.
Any idea why the server explorer data connection works, but not the c# code, using the same connection string?
By the way, something went wrong earlier and I seemed to be missing my odbc drivers. I downloaded and ran AccessDatabaseEngine.exe and AccessDatabaseEngine_X64.exe, both with /quiet option so they would properly install. This left me with the odbc drivers available, but still I get the above exception.
UPDATE: The same error occurs when trying to open connection to a MySQL database using a known good DSN entry for MySQL. This DSN is working when using it from a production Access application.
Thanks...
Finally solved this issue by completely uninstalling my 32-bit Office and installing 64-bit office.

Connecting .accdb access database to QGIS

I am trying to link an access database that is .accdb not .mdb . What do I need to do? where am I going wrong?
I have already tried to create it under the ODBC data source admin 32-bit (System DSN) tab. I selected the Driver - Microsoft Access Driver (*.mdb, *.accdb) Data source name = Faunafile and selected the database which I have saved in my c drive. System database is None.
The in QGIs I click on "add vector". click database. in type I choose ESRI Personal GeoDatabase and under connections click New.
Type = ESRI Personal Geodatabase. Name is Faunafile and Database = Faunafile. Then when I try to test connect it comes up with an error message.
This is the error message I get:
Connection failed - Check settings and try again.
Extended error information:
Unable to initialize ODBC connection to DSN for Faunafile,
[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
Maybe you're using QGis 64-bits. Try to uninstall it and install QGis 32-bits. I had the same problem and now it works.

Access MySQL database in R on Mac OS X

I am having problems accessing my MySQL database from R on mac (Mac OS X 10.11.2). The following code:
library("RODBC")
mycom <- odbcConnect("wsn_mysql_5", uid="root", pwd="*****")
Gives this error:
1: In RODBC::odbcDriverConnect("DSN=wsn_mysql_5;UID=root;PWD=****") :
[RODBC] ERROR: state 00000, code 15509574, message
[iODBC][DriverManager]dlopen(/usr/local/lib/libmyodbc5a.so,
6): no suitable image found. Did find:
/usr/local/lib/libmyodbc5a.so: mach-o, but wrong architecture
2: In RODBC::odbcDriverConnect("DSN=wsn_mysql_5;UID=root;PWD=****") :
[RODBC] ERROR: state IM003, code 15509574, message
[iODBC][Driver Manager]Specified driver could not be loaded
What I have tried so far:
I installed iODBC driver manager 3.52.12.
I installed MySQL connector odbc-5.3.6-osx10.11-x86-64bit and created a DSN (server=localhost, user=root, password=*****,database=wsn_db).
I was getting this error when testing it in iODBC Administrator:
[iODBC][Driver manager]dlopen(/usr/local/lib/libmyodbc5w.so, 6): no suitable image found.
Did find: /usr/local/lib/libmyodbc5w.so: mach -o, but wrong architecture.
How to add ODBC to MAMP on OSX led me to think that is was a 32-bit vs 64-bit problem.
So I installed MySQL connector odbc-5.2.7-osx10.7-x86-32bit which is the most recent 32-bit version I found. When I test this with iODBC Administrator, it seems to work, but I get the error mentioned at the beginning when I try the code in R.
The FAQ of iODBC says the following regarding this type of error:
[iODBC] [Driver Manager]Specified driver could not be loaded
There are a few reasons why this could occur, and thinking through the architecture helps. Your application has loaded libiodbc successfully, and it has found an odbc.ini file (or equivalent through the ODBCINI environment variable), and it has found a DSN within that odbc.ini that matches the name requested in your connection.
However, the driver manager has had problems loading the library specified in the `Driver=' line of that DSN definition. Either it doesn't exist, or its permissions are insufficient to allow your application to load it (it must be readable and executable, and the directories leading down to it must be executable), or maybe the file is not a dynamic library - it could be a static library (a *.a file except on AIX) or is otherwise corrupted. These are all things to check, or you may be best off reinstalling the driver if all the permissions check out.
So I checked the permission, but they were correct.
I also reinstalled the driver, but it didn't change anything.
After reading this, I checked odbc.ini and obdcinst.ini.
I changed odbc.ini to:
[ODBC Data Sources]
wsn_mysql_7 = wsn_mysql_7
[ODBC]
TraceLibrary=
[wsn_mysql_7]
Driver = /usr/local/lib/libmyodbc5w.so
DATABASE = wsn_db
DESCRIPTION = DSN for wsn_db in R
SERVER = localhost
UID = root
PASSWORD = *****
and odbcinst.ini to
[ODBC Drivers]
MySQL ODBC 5.2 ANSI Driver=Installed
MySQL ODBC 5.2 Unicode Driver=Installed
[ODBC Connection Pooling]
PerfMon=0
Retry Wait=
[MySQL ODBC 5.2 ANSI Driver]
Driver = /usr/local/lib/libmyodbc5a.so
Description = mySQL driver
Threading = 0
[MySQL ODBC 5.2 Unicode Driver]
Driver = /usr/local/lib/libmyodbc5w.so
Description = mySQL driver
Threading = 0
But it didn't seem to change anything.
I also tried an alternative RODBC code based on this question.
mycom <- odbcDriverConnect("Provider=MSDASQL;Driver={MySQL ODBC 5.2 UNICODE Driver};
Server=localhost;database=wsn_db;Uid=root;Pwd=*****;Option=3")
I am not sure what to do next? Even a completely different way to access the mySQL data in R would help.
I finally manage to have a working configuration by using unixODBC and using a 64-bit ANSI connector. If you are trying to connect to a MySQL database with RODBC I recommend:
To follow this tutorial to install unixODBC and RODBC
Install the latest MySQL ODBC connector. I installed mysql-connector-odbc-5.3.6-osx10.11-x86-64bit. I had to install from tar since the installer didn't work. The instructions are here.
I used ODBC Administrator to configure the DSN, but I guess the instruction from [Hiltmon's tutorial] might work as well.
I first used the Unicode driver, but got the following error:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=wsn_db_dsn;UID=root;PWD=****") :
[RODBC] ERROR: state H, code 0, message [unixODBC][
2: In RODBC::odbcDriverConnect("DSN=wsn_db_dsn;UID=root;PWD=****") :
ODBC connection failed
I corrected this by selecting the ANSI driver as suggested in RODBC error - ODBC connection failed - can't connect to MySQL with my mac (mavericks).
I hope this can save time to someone.

Error Trying to Connect R to MySQL using RODBC

I am attempting to connect R to a MySQL database via RODBC (in Win 7 environment), but cannot get past the following error:
[RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
After entering the following command:
odbcConnect(dsn,uid="My_UID",pwd="My_PWD")
I have successfully created a driver for the database by following the odbc wizard, and have installed the RODBC package into RStudio (ver 0.98.490). The MySQL is running on 32bit, I have the 32bit driver installed and working, and my RStudio is also 32bit. I know that the driver is working since both the test connection in config. works, as do queries run on excel using the driver.
What can I do to get the connection to work in R?
(Previous responses to this question seem to have been removed.)
Thanks in advance!
I faced a similar RODBC connectivity issue using DSN, where R was crashing abruptly without any logs/error trace. I tried with DSN-less ODBC connection API, it works fine.
odbcDriverConnect("SERVERNAME=;DRIVER={};DATABASE=;UID=;PWD=;")
Steps:
1. Install ODBC driver
2. Define VNode (host, listen address, protocol)
3. Use the odbcDriverConnect API
Docs:
http://cran.r-project.org/web/packages/RODBC/RODBC.pdf
http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf
Hope this helps.