pyodbc connection issue with psqlodbc driver on osx - python

I'm trying to connect to my local postgres database via pyodbc(i'd like to use MySQL workbench to import/view a postgres schema eventually), and I can't seem to get anything to find my psqlodbc driver. I've brew install psqlodbc, and I have the Driver and Setup files, /usr/local/lib/psqlodbcw.so and /usr/local/lib/psqlodbca.so, respectively.
Using the pyodbc library in python, I've tried to connect, but pyodbc can't find my driver
conn_str = (
"DRIVER={psqlodbc};"
"DATABASE=postgres;"
"UID=postgres;"
"PWD=postgres;"
"SERVER=localhost;"
"PORT=5432;"
)
conn = pyodbc.connect(conn_str)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'psqlodbc' : file not found (0) (SQLDriverConnect)")
My ~/.odbc.ini looks like:
1 [PostgreSQL]
2 Description = PostgreSQL driver for Unix
3 Driver = /usr/local/lib/psqlodbcw.so
4 Setup = /usr/local/lib/psqlodbca.so
El Capitan version 10.11 and i get:
file /usr/local/lib/psqlodbcw.so
/usr/local/lib/psqlodbcw.so: Mach-O 64-bit bundle x86_64
file /usr/local/lib/psqlodbca.so
/usr/local/lib/psqlodbca.so: Mach-O 64-bit bundle x86_64
Not sure what I should do from here. Could this be a configuration or symlink issue?

Related

pyodbc issue: ODBC driver not found on MacOS

My environment
Python: 3.9.7
pyodbc: 4.0.32
OS: MacOS 12.0.1, Apple M1 Max
DB: Azure SQL
driver: ODBC Driver 17 for SQL Server
Running
import pyodbc
server = 'myserver.database.windows.net'
database = 'mydb'
username = 'myuser'
password = 'mypassword'
odbc_driver = '{ODBC Driver 18 for SQL Server}'
conn_str = (
f"Driver={odbc_driver};"
f"Server=tcp:{server},1433;"
f"Database={database};"
f"Uid={username};"
f"Pwd={password};"
"Encrypt=yes;"
"TrustServerCertificate=no;"
"Connection Timeout=30;")
print(conn_str)
cnxn = pyodbc.connect(conn_str)
outputs
Driver={ODBC Driver 18 for SQL Server};Server=tcp:myserver.database.windows.net,1433;Database=mydb;Uid=myuser;Pwd=mypassword;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;
Traceback (most recent call last):
File "/Users/....", line 21, in <module>
cnxn = pyodbc.connect(conn_str)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 18 for SQL Server' : file not found (0) (SQLDriverConnect)")
Using the same connection string in isql:
isql -v -k "Driver={ODBC Driver 18 for SQL Server};Server=tcp:<server>,1433;Database=<db>;Uid=<user>;Pwd=<pw>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
returns Connected and queries are possible
Drivers are on my machine, I tried first with ODBC Driver 17 for SQL Server, then installed ODBC Driver 18 for SQL Server with same result.
How can I fix this? Created a GitHub issue as well here
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib
'ODBC Driver 17 for SQL Server' : file not found (0)
(SQLDriverConnect)")
According to the Above Error drivers are properly not installed .However, for ODBC Driver 17 for SQL server ,Please follow the official document Installing the Microsoft ODBC Driver for SQL Server on MacOS.
Another way to Solve an Error, check whether your driver is installed or not then, follow the driver path
Example : "/user/local/lib/libmsodbcsql.17.dylib" , Replace Driver value as shown in below code.
cnxn = pyodbc.connect('DRIVER={/user/local/lib/libmsodbcsql.17.dylib};SERVER=ServerName,1433;DATABASE=DatabaseName;UID=Username;PWD=password')
Reference:
https://docs.snowflake.com/en/user-guide/odbc-mac.html#installing-and-configuring-the-odbc-driver-for-macos
/bin/bash -c "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-
mssql-release
brew update
brew install msodbcsql#13.1.9.2 mssql-tools#14.0.6.0
These commands is for MacOS exactly. You can check here https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver16

Error: `pyodbc.drivers()` returns empty list inside VSCode

I followed Microsoft instructions for installing its ODBC driver and prepared a conda environment with the necessary packages to work with azure SQL database with Python, including pyodbc. I used the instructions for Ubuntu because I'm on Linux Mint 20 and well, that option isn't available (and Linux Mint is based on Ubuntu).
Then I prepared a small script to connect to a test azure database that I created:
import pyodbc
configString = (
f'DRIVER={ODBC Driver 17 for SQL Server};'
f'SERVER={server};'
'PORT=1433;'
f'DATABASE={database};'
f'UID={username};'
f'PWD={password}'
)
with pyodbc.connect(configString) as conn:
with conn.cursor() as cursor:
cursor.execute('SELECT TOP 3 name, collation_name FROM sys.databases')
row = cursor.fetchone()
while row:
print (str(row[0]) + ' ' + str(row[1]))
row = cursor.fetchone()
This fails with the error ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)").
But the file exists, and if I run this from just a terminal (instead of from within VSCode) it works fine.
Also, pyodbc.drivers() resturns [] from within VSCode but it returns the correct value ['ODBC Driver 17 for SQL Server'] when called from a Python shell within a terminal.
So the question is: why pyodbc fails to get the drivers from within VSCode but gets the correct values from a terminal?

InterfaceError with sqlalchemy and Microsoft Access Driver in Python

I want to connect to a MS Acces database that is on my host system.
I am using Python 3.7 in jupyter notebook. When I connect to the engine, I get the exception InterfaceError.
My Code:
import urllib
from sqlalchemy import create_engine
connection_string = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=./Datenbank1.accdb;'
)
connect_str = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connect_str, echo=True)
engine.connect()
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Der Datenquellenname wurde nicht
gefunden, und es wurde kein Standardtreiber angegeben (0)
(SQLDriverConnect)') (Background on this error at:
http://sqlalche.me/e/13/rvf5)
The data source name was not found and no default driver was specified (0) (SQLDriverConnect)')
Can you help me to find the error?
Maybe I am missing the right driver but I don't know how to install it
On Windows, one way to tell if a Python script is running as 32-bit or 64-bit is to check the output from pyodbc.drivers(). In 32-bit mode, the list will include the older "Jet" driver …
Microsoft Access Driver (*.mdb)
… (note: no mention of *.accdb) which ships with Windows and is only available as 32-bit.
In this case the list returned by pyodbc.drivers() did not include that driver so we can deduce that the script is running as 64-bit. Therefore the solution is to download the 64-bit version of the newer "ACE" ODBC driver from here.
For more details on connecting to Access via pyodbc, see the pyodbc wiki.

Sym linking issue connecting to Azure Database from macOS

I am trying to get the data from my SQL Azure Database. It seems like my python code throws the error because of linking confusion with ODBC Drivers.
Here is my Python code.
from urllib import parse
from sqlalchemy import create_engine
connecting_string = 'Driver={ODBC Driver 13 for SQL Server};Server=tcp:mftaccountinghost.database.windows.net,1433;Database=mft_accounting;Uid=localhost;Pwd=######;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
params = parse.quote_plus(connecting_string)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
result = connection.execute("select 1+1 as res")
for row in result:
print("res:", row['res'])
connection.close()
Here is the error I get:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/usr/local/lib/libmsodbcsql.13.dylib' : file not found (0) (SQLDriverConnect)")
When I check my terminal I get the following results. Cannot resolve this issue...
Here is my connection string for the server:
Here is complete guide to install the ODBC driver in UNIX machine:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
If you installed the v17 msodbcsql package that was briefly available, you should remove it before installing the msodbcsql17 package. This will avoid conflicts.
Try uninstalling the driver and install it again it should work.
Also check below thread for additional reference:
Can't open lib 'ODBC Driver 13 for SQL Server'? Sym linking issue?
Hope it helps.

Create a DSN for pyodbc similar to PHP PDO. Is it possible?

I use PHP PDO to connect a MySql Db and it works great. I have something like:
$dsn = 'mysql:host=localhost;dbname=database_name';
$user_db = 'admin';
$password = 'password';
$pdo = new PDO($dsn, $user_db, $password);
Now I need to load same database from a python script and I have to use pypodbc module
But I'm getting some issue:
If I do (on Python):
pyodbc.connect('DRIVER={MySQL};SERVER=localhost;DATABASE=database_name;UID=admin;PWD=password;')
I got en error on log:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open
lib '/usr/lib64/libmyodbc5.so' : file not found (0)
(SQLDriverConnect)")
If i check the /etc/odbcinst.ini i can see:
# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
I tried to add the the package mysql-connector-odbc by YUM and i got mysql-connector-odbc.x86_64 0:5.1.5r1144-7.el6
And then running my script I got a new error:
/usr/local/bin/python2.7: relocation error: /usr/lib64/libmyodbc5.so: symbol strmov, version libmysqlclient_16 not defined in file libmysqlclient_r.so.16 with link time reference
It seems this version is not compatible with MySql which I have: 5.5.37-cll - MySQL Community Server (GPL)
I did a YUM REMOVE to restore previous conf.
And now ? Any suggestions ? Thanks!
My configuration:
My Server: CENTOS 6.6 x86_64 virtuozzo
MySql: 5.5.37-cll - MySQL Community Server (GPL)
Finally I fixed it!
yum install unixODBC-devel
yum install mysql-connector-odbc
yum install openssl098e
and than:
rpm -ivh libmysqlclient16-5.1.69-1.w5.x86_64.rpm
Now the
pyodbc.connect('DRIVER={MySQL};SERVER=localhost;DATABASE=database_name;UID=admin;PWD=password;')
works!! yeah!

Categories

Resources