Reading an .mdb file into R - ms-access

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

Related

odbcConnect with Access database difference between R and RStudio

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.

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.

SSRS Issues with 32bit and 64bit ODBC drivers

The ultimate goal is for users to be able to run a report that looks pretty and grabs current information from our database. We'd like to use SQL Report Builder since we're already using it for other reports. The database is Cisco UCCX and we're accessing it with an ODBC connection from our reporting services SQL Server 2008 R2.
We've successfully setup System ODBC connections with both 64bit and 32bit drivers. When trying to access the connections though, we're receiving errors.
Using the 32bit driver, we try to create a Data Source in SSRS for use by Report Builder and receive the error:
"ERROR [IM014] [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application"
Using the 64bit driver, we can successfully create and test the ODBC connection as a Data Source, but then when we attempt to create a Dataset with it in Report Builder, we get this error:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Error received using the 64bit driver for ODBC connection
I ran into this recently. You need to have the same name for both 32 and 64bit DSNs. It's annoying because SSRS can run 64bit, but reportbuilder only exists in 32bit. So parts of reportbuilder seem to work (like running queries), but some don't (like refreshing query fields). Just have a DSN for both 32bit and 64bit, then make sure they're both the same name and the problems should stop.
You may be hitting an old but recurring issue with minor corruption in the Windows Registry.
The corruption takes the form of entries containing this 4-character string —
#=""
These entries aren't visible anywhere except Registry export files — the Registry Editor ignores them completely — but they can lead to a number of undesired behaviors, including the error you report.
NOTE: On your 64-bit Windows machine, there are naturally some complications tied to the 32-bit Registry. This Microsoft KB article may be sufficient to get you through these.
I suggest that you use the 64-bit Registry Editor (%systemroot%\system32\regedit) to export the following branches (where these problematic entries tend to be found) —
HKEY_LOCAL_MACHINE\Software\ODBC
HKEY_CURRENT_USER\Software\ODBC
HKEY_LOCAL_MACHINE\Software\WOW6432Node\ODBC
HKEY_CURRENT_USER\Software\WOW6432Node\ODBC
Edit these files in any text editor (Notepad or Wordpad are generally fine), and delete all lines which consist of that 4-character string, above. Then, delete the Registry tree segment(s) you exported, and import from the edited files — thereby restoring the tree segment(s), minus the corruption.
It won't hurt to repeat the above process with the 32-bit Registry Editor (%systemroot%\syswow64\regedit), but as you've described the issue, I don't think you'll find any #="" in the 32-bit export.

pyodbc + MySQL + Windows: Data source name not found and no default driver specified

I am trying to connect to MySQL 5.6 on a Windows Server 2008 R2 localhost with pyodbc. I used the full installation for the MySQL instance on the localhost, including the ODBC connector. I have it connecting to a remote SQL Server instance beautifully, but for the life of me I can't get it to connect to the local MySQL instance. I am using this guide from connectionstrings.com as reference.
Here's some code:
import pyodbc
def create_mssql_conn():
return pyodbc.connect(r'Driver={SQL Server};Server=MSSQLSRV;Database=ecomm;Trusted_Connection=yes;')
def create_mysql_conn():
return pyodbc.connect(r'Provider=MSDASQL;Driver={MySQL ODBC 5.6 UNICODE Driver};Server=127.0.0.1;Database=ecomm;User=root;Password=myP#$$w0rd;Option=3;')
# conn = create_mssql_conn() # This one works
conn = create_mysql_conn() # This one breaks
cursor = conn.cursor()
cursor.execute('SELECT * FROM inventory')
while 1:
row = cursor.fetchone()
if not row:
break
print row
Here is the error:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I tried everything from adding Provider=MSDASQL; to changing ANSI to UNICODE in the connection string. Any suggestions?
I was having a similar issue. I am using windows 8, and mysql.
The way I solved the problem was by going into my
control panel>Systems and Security>Administrative Tools.>ODBC Data Sources
Either the 32 bit or 64 bit version depending on your computer.
Then you click on the System DNS file. If you do not see any MySQL driver you have to click ADD. It brings up a list, from that list select the MySQL driver.
For me it was MySQL ODBC 5.3 ANSI(they have a unicode driver also). Click finish. Once you do that then you have to change your connection line in your code to the corresponding Driver that you just filled out.
Ex:
def create_mysql_conn():
return pyodbc.connect(r'Driver={MySQL ODBC 5.3 ANSI Driver};Server=MSSQLSRV;Database=ecomm;Trusted_Connection=yes;')
This should work, or at least it solved my connection issue because I was getting all sorts of different errors with everything I tried. This was what solved the issue for me.
In Win64 there are two ODBC environments: 32 and 64 bit and you can work with Python 32 bit or Python 64 bit. Check what version of Python you use and then use proper version of odbcad32.exe (32 bit version is located in the SysWoW64 directory) to create SystemDSN. With SystemDSN you can check if you can connect to the database. Then try to connect to DB from Python code.
You can list ODBC datasources available to pyodbc via my code recipe: https://code.activestate.com/recipes/578815-printing-list-of-odbc-data-sources-with-pyodbc-mod/
Head to Administrative Tools and the ODBC Data Sources. You should click on the DNS file. Most likely you'd see Microsoft Access and excel as the only data sources available, so you need to add your SQL Server. Mine was SQL Server 2008 R2, and so far, it has worked.
For the error mentioned
"pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')"
I installed the 'Microsoft Access Database Engine 2010 Redistributable 64-bit' as I have 64-bit system and then added the DSN in the directory of MS Access database and it's working now.
Here is the link of the video for how to create DSN and connect to python.
https://www.youtube.com/watch?v=zw9P2wSnoIo

Mysql table on Excel sheet?

I am using Windows7 and Microsoft office 2010 and mysql5. I want to connect Microsoft excel with my database mysql. I have installed all the drivers. I have created a data source as well. I am going step-by-step to connect, but at the end when i click on test connections, I receive this error message
Test connection failed because of error in intializing provider unspecified error.
You probably are not using the right ODBC driver.
Please check if you downloaded the one that matches your MySQL database. (not only version number, but especially if it is a 32-bit server or a 64-bit server).