Establishing MS Access connection with UnixODBC and FreeTDS on Mac - r

I've been trying to establish a connection to an MS Access database I have on my local hard drive using FreeTDS and UnixODBC. My ultimate goal is to open the connection in R via RODBC and implement some SQL scripts developed for this specific database to extract data. I've followed advice from this page (How do I install RODBC on Mac OS X Yosemite with unixodbc and freetds?), but am still having trouble.
When I implement isql in terminal I get the following error message.
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect
I'm assuming my error is in how I've identified the host in my various setup files, which are as follows.
freetds.conf
[global]
; tds version = 8.0
; dump file = /tmp/freetds.log
; debug flags = 0xffff
; timeout = 10
; connect timeout = 10
text size = 64512
[my_db]
# insert the actual host below
host = My_computer_name.local
port = 1433
tds version = 8.0
odbc.ini
[my_db]
Driver = MSSQL
Servername = My_computer_name.local
Port = 1433
Database = /filepath_to_db/my_db.mdb
TDS_Version = 8.0
odicinst.ini
[MSSQL]
Description = Microsoft SQL Server driver
Driver = /usr/local/Cellar/freetds/1.00.39/lib/libtdsodbc.so
Setup = /usr/local/Cellar/freetds/1.00.39/lib/libtdsodbc.so

FreeTDS is for connecting to Microsoft SQL Server and Sybase databases. It is not designed to work with Microsoft Access databases.

Related

Problems Connecting to Database within Docker Image via RStudio using DBI

I'm attempting to connect to a database within a local Docker image I've spun up using DBI and am running into some pretty cryptic errors... I've successfully started the Docker container (below shows result of docker ps -a):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
131ab042012c mcr.microsoft.com/mssql/server:2017-latest "/opt/mssql/bin/nonr…" 27 minutes ago Up 27 minutes 127.0.0.1:1433->1433/tcp admiring_northcutt
I then try to connect via:
pool<- pool::dbPool(odbc::odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = "localhost",
Database = "master",
UID = "sa",
PWD = "***********",
Port = 1433)
and receive the error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute
Checking my odbc driver using odbc::odbcListDrivers() returns the output below, so the drivers seem to be present.
name attribute value
1 ODBC Driver 17 for SQL Server Description Microsoft ODBC Driver 17 for SQL Server
2 ODBC Driver 17 for SQL Server Driver /usr/local/lib/libmsodbcsql.17.dylib
3 ODBC Driver 17 for SQL Server UsageCount 2
In addition, executing the container and attempting to connect to the database via the commands:
docker exec -it <container-name> /bin/bash
/opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P '<password>'
EDIT: The above method now works and I'm able to actively query within the database. I know that the error can't be due to invalid login credentials because I'm using the same exact ones as I ran above in the container.
This same process works for a coworker, so I'm wondering if it's a local issue with my set up. We already compared our .ini files as suggested in this link and they're identical, I've clean re-installed Docker, and I've even ran the coworker's identical container with no luck. Any ideas what could be happening here?
Computer specs:
MacBook Pro (16-inch 2019)
OS: macOS Monterey (version 12.2.1)
Processor: 2.3 GHz 8-Core Intel Core i9
Memory: 16GB 2667MHz DDR4
Startup Disk: Macintosh HD
Graphics: AMD Radeon Pro 5500M 4 GB
Rstudio Version: 2021.09.0 Build 351
OpenSSL Version: 1.1

Can't connect to PostgreSQL in Rstudio, possible permissions issue?

I've followed the documentation on db.rstudio.com and other sites, and I'm not able to connect to my Postgres database from Rstudio. I'm on macos 10.14. Running:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "PostgreSQL Driver",
Database = "mydatabase",
UID = "myuser",
PWD = "mypassword",
Host = "localhost",
Port = 5432)
Gives the error:
Error: nanodbc/nanodbc.cpp:983: 00000:
At first, I got an empty list when running odbcListDrivers(), however I was able to get it to show the postgres driver location/value by copying odbcinst.ini from the global /etc to my user (cp /etc/odbcinst.ini ~/.odbcinst.ini).
Similarly, odbcListDataSources() returned nothing until I did the same thing (cp /etc/odbc.ini ~/.odbc.ini)
The odbc.ini and odbcinst.ini are locked from editing via Finder, but can be edited via nano/terminal.
I also tried to solve the problem by installing the database drivers via Postgres' stack builder, but it didn't change anything.
Overall I'm suspecting that it's a permissions issue, but I'm not quite sure where to look next.
Thanks for your help!
The permission for the odbc.ini was:
-rw-rw-rw-# 1 root wheel 139B Mar 31 12:57 odbc.ini
Which I changed to:
-rwxr-xr-x# 1 myuser wheel 139B Mar 31 12:57 odbc.ini
But that didn't solve the problem, the error is the same.
The content of odbcinst.ini is:
[PostgreSQL Driver]
Driver = /usr/local/lib/psqlodbcw.so
The content of odbc.ini is:
[PostgreSQL]
Driver = PostgreSQL Driver
Database = mydatabase
Servername = localhost
UserName = myuser
Password = mypassword
Port = 5432
odbc::odbcListDrivers() gives:
name attribute
1 PostgreSQL Driver Driver
2 ODBC Drivers Amazon Redshift
3 Amazon Redshift Driver
value
1 /usr/local/lib/psqlodbcw.so
2 Installed
3 /opt/amazon/redshift/lib/universal/libamazonredshiftodbc.dylib
odbc::odbcListDataSources() gives:
name description
1 PostgreSQL PostgreSQL Driver

"[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN

Getting the "[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN referenced in the connection string. Why would this be happening and what can be done?
Trying to set up pyodbc for connecting to MSSQLServer using the docs (centos 7). Yet, when trying to actually connect to the database,
import pyodbc
# setup db connection
server = 'myserver'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn_str = 'DSN=MyMSSQLServer;DATABASE='+database+'UID='+username+'PWD='+password+'MultipleActiveResultSets=True;'
cnxn = pyodbc.connect(cnxn_str)
getting error "[unixODBC][Driver Manager]Data source name not found, and no default driver specified", even though when running:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer] Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=<now using my sql server ip>
I can see that the DSN that I referenced in the python code is recorded in the /etc/odbc.ini file. Does anyone know what could be going on here? Thanks.
Note: I initially ran the pyodbc setup exactly as specified in the docs, but later re-did this step to use the ip address of the sql server I wanted to connect to:
vi /home/user/odbcadd.txt
[MyMSSQLServer] Driver = ODBC Driver 13 for SQL Server
Description = My MS SQL Server
Trace = No
Server = <my sql server ip>
and just re-ran the write to odbc.ini:
sudo odbcinst -i -s -f /home/user/odbcadd.txt -l
The solution commented by user Gord Thompson solved my problem. The ~/odbcadd.txt file that I was using to fill the /etc/odbc.ini file needed to have the 'Driver=' line on a separate line from the DSN to look like:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer]
Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=172.18.4.38

pyodbc.connect to FreeTDS connection requires explicit PORT=1433;

I have a python script running on python 2.7 in CentOS 2.6 that connects to a Sql erver database:
pyodbc.connect("DRIVER=FreeTDS;SERVER=someServer;DATABASE=someDb;UID=myUser;PWD=superSecret;CHARSET=UTF8;TDS_Version=7.2")
That call will fail with the following: pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
The freetds trace will say login.c:436:invalid port number
Adding PORT=1433; will cause the connection to succeed even though that is the default port number, and I added the following to my freetds.conf:
[global]
# TDS protocol version
tds version = 7.0
port = 1433
How do I make FreeTDS try port 1433 as the default port so I don't have to set it in the query string?
Instead of this, as you noted:
pyodbc.connect("DRIVER=FreeTDS;SERVER=someServer;DATABASE=someDb;UID=myUser;PWD=superSecret;CHARSET=UTF8;TDS_Version=7.2")
...you could put these values in your connection string, like this:
pyodbc.connect("DRIVER={FreeTDS};SERVER=someServer;PORT=1433;DATABASE=someDb;UID=myUser;PWD=superSecret;TDS_Version=7.2;CHARSET=UTF8")
For your freetds.conf configuration:
[global]
client charset = UTF-8
tds version = 7.2
[someServer]
host = someServer
port = 1433
tds version = 7.2
Both ways have always worked for me.
On a side note, I've started using this driver, made by Microsoft specifically for RedHat and CentOS (although I don't know if it will work on a version as old as yours):
https://msdn.microsoft.com/en-us/library/hh568451%28v=sql.110%29.aspx
Best of luck!

Ubuntu Shiny server connecting to Jet/ACE databases

Can it be done: Reading data stored in an MS Access (.accdb) database, from within Shiny apps running on Ubuntu Shiny server?
We have no knowledge of SQL Server Express. We have our data organized in simple MS Access databases, and want to deploy our Shiny apps (who visualize this data) on an Ubuntu Shiny server.
It all works on our local Windows machines, but how to make it also work with an Ubuntu Shiny server?
I understand that with our minimal knowledge of database systems, it is not straightforward to go porting our databases to SQL Server Express.
Thanks in advance for your expertise!
I had a bit of a job setting this up myself. I had to take info from several sources to get all the required packages – the following is a list of good info sources:
http://guywyant.info/log/206/connecting-to-ms-sql-server-from-ubuntu/
http://driftharmony.wordpress.com/2008/08/15/connecting-ubuntu-804-to-microsoft-sql-server/
https://code.google.com/p/django-pyodbc/wiki/FreeTDS
FreeTDS working, but ODBC cannot connect
The 3 files were ultimately configured thus:
Detail of file: /etc/odbc.ini
[NameThis]
Driver = FreeTDS
TDS_Version=8.0
Servername = YourServer
Port = 1433
Database = testing
Trace = No
Detail of file: /etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Detail of file: /etc/freetds/freetds.conf
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
# This file is installed by FreeTDS if no file by the same name is found in the installation directory.
# For information about the layout of this file and its settings, see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database server specific section
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field. Try setting 'text size' to a more reasonable limit
text size = 64512
# Test Kx
[NameThis]
host = YOUR IP
port = 1433
tds version = 7.2

Resources