Connection issues on OSX via pyodbc - python

I'm trying to write a python app (python 3.5 with pyCharm IDE) that put multiple queries and do math etc... It needs to run on both mac and windows. On windows side because of pymssql won't work i tried pyodbc which worked flawlessly (it's the opposite on mac side). But on the mac side whenever i try to connect with this code:
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=myServerIp;DATABASE=myDatabaseName;UID=myUserName;PWD=myPassword')
cursor = cnxn.cursor()
it gives the error of: pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'odbc.ini' : file not found (0) (SQLDriverConnect)")
Normally i can connect MS SQL from excel or any other query software on mac. I'm using Actual SQL Server driver on system wide.
So please guide me to fix this problem either using Actual SQL Server drivers (which is paid version) or properly configure python or unixodbc (not sure which is the problem). Thank you and regards
PS: I installed both pyodbc, unixodbc and freetds via pip as i followed documentation
Update1:
$ cat $(odbc_config --odbcinstini)
[ODBC]
DEBUG=1
TraceFile=/home/ftp/sqltrace.log
Trace=Yes
[FreeTDS]
Description=MSSQL Driver
Driver=/usr/local/lib/libtdsodbc.so
$ odbc_config --ulen
-DSIZEOF_SQLULEN=8
$ odbc_config --libs
-L/usr/local/Cellar/unixodbc/2.3.2_1/lib -lodbc
$ odbc_config --prefix
/usr/local/Cellar/unixodbc/2.3.2_1
$ odbc_config --odbcinstini
/usr/local/Cellar/unixodbc/2.3.2_1/etc/odbcinst.ini
$ odbc_config --odbcini
/usr/local/Cellar/unixodbc/2.3.2_1/etc/odbc.ini
$ odbc_config --version
2.3.2

uninstalling and installing again with this command fixed the issue:
brew install freetds --with-unixodbc

Related

Python connect to sql server from anaconda environment on mac give error

I want to connect python to sql server on mac M1 , I installed all the required files by microsoft using homebrew , when I'm not in anaconda environment the isql command and pyodbc runs successfully when I activate conda environment I got this error :
[01000][unixODBC][Driver Manager]Can't open lib '/opt/homebrew/lib/libmsodbcsql.18.dylib' : file not found
Please if anyone could help !

Connecting to Azure SQL with Python

I am trying to connect to a SQL Database hosted in Windows Azure through MySQLdb with Python.
I keep getting an error mysql_exceptions.OperationalError: (2001, 'Bad connection string.')
This information works when connecting through .NET (vb, C#) but I am definitely not having any luck here.
For below I used my server's name from azure then .database.windows.net Is this the correct way to go about this?
Here is my code:
#!/usr/bin/python
import MySQLdb
conn = MySQLdb.connect(host="<servername>.database.windows.net", user="myUsername", passwd="myPassword", db="db_name")
cursor = conn.cursor()
I have also tried using pyodbc with FreeTDS with no luck.
#Kyle Moffat, what OS are you on? Here is how you can use pyodbc on Linux and Windows:
https://msdn.microsoft.com/en-us/library/mt763261(v=sql.1).aspx
Windows:
Download and install Python
Install the Microsoft ODBC Driver 11 or 13:
v13: https://www.microsoft.com/en-us/download/details.aspx?id=50420
v11: https://www.microsoft.com/en-us/download/details.aspx?id=36434
Open cmd.exe as an administrator
Install pyodbc using pip - Python package manager
cd C:\Python27\Scripts>
pip install pyodbc
Linux:
Open terminal
Install Microsoft ODBC Driver 13 for Linux For Ubuntu 15.04 +
sudo su
wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh 
sh installodbc.sh
For RedHat 6,7
sudo su
wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-SQL-8d067754/file/153653/4/install.sh
sh install.sh
Install pyodbc
sudo -H pip install pyodbc
Once you install the ODBC driver and pyodbc you can use this Python sample to connect to Azure SQL DB
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT ##version;")
row = cursor.fetchone()
while row:
print row[0]
row = cursor.fetchone()
If you are not able to install the ODBC Driver you can also try pymssql + FreeTDS
sudo apt-get install python
sudo apt-get --assume-yes install freetds-dev freetds-bin
sudo apt-get --assume-yes install python-dev python-pip
sudo pip install pymssql==2.1.1
Once you follow these steps, you can use the following code sample to connect:
https://msdn.microsoft.com/en-us/library/mt715796(v=sql.1).aspx

PyODBC : can't open the driver even if it exists

I'm new to the linux world and I want to query a Microsoft SQL Server from Python. I used it on Windows and it was perfectly fine but in Linux it's quite painful.
After some hours, I finally succeed to install the Microsoft ODBC driver on Linux Mint with unixODBC.
Then, I set up an anaconda with python 3 environment.
I then do this :
import pyodbc as odbc
sql_PIM = odbc.connect("Driver={ODBC Driver 13 for SQL Server};Server=XXX;Database=YYY;Trusted_Connection=Yes")
It returns :
('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0' : file not found (0) (SQLDriverConnect)")
The thing I do not undertsand is that PyODBC seems to read the right filepath from odbcinst.ini and still does not work.
I went to "/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0" and the file actually exists !
So why does it tell me that it does not exist ?
Here are some possible clues :
I'm on a virtual environment
I need to have "read" rights because it's a root filepath
I do not know how to solve either of these problems.
Thanks !
I also had the same problem on Ubuntu 14 after following the microsoft tutorial for SQL Server Linux ODBC Driver.
The file exists and after running an ldd, it showed there were dependencies missing:
/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version GLIBCXX_3.4.20' not found (required by /opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0)
/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: versionCXXABI_1.3.8' not found (required by
after searching for a while I found its because Ubuntu's repo didnt have GLIBCXX on version 3.4.20, it was at 3.4.19.
I then added a repo to Ubuntu, updated it and forced it to upgrade libstdc++6
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libstdc++6
Problem solved, tested with isql:
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
After that I tried testing using pdo_odbc (PHP), it then gave me the same driver not found error.
To solve this I had to create a symbolic link to fix libodbcinst.so.2:
sudo ln -s /usr/lib64/libodbcinst.so.2 /lib/x86_64-linux-gnu/libodbcinst.so.2
I had the same problem 'file not found (0) (SQLDriverConnect)' on MAC OS with the following code
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=myServerIP,1433;DATABASE=myDBName;UID=sa;PWD=dbPassword')
after googling for two days, I cannot fix the issue even modify the freetds.conf, odbcinst.ini and odbc.ini
finally, I found the solution via replacing DRIVER value
cnxn =
pyodbc.connect('DRIVER={/usr/local/lib/libmsodbcsql.13.dylib};SERVER=myServerIP,1433;DATABASE=myDBName;UID=sa;PWD=dbPassword')
My dev environment
MAC OS El Capitan
python 3.6.1 in Anaconda
I found an answer that works for me here. This is for python 2.7 (so may not work for those who are looking for a solution for python 3.x).
The suggested solution is to update libgcc: 4.8.5-2 --> 5.2.0-0
For updating libgcc, use this command
conda update libgcc
The following suggestions may help to solve the problem:
Make sure the drive configuration INI file exists odbcinst -j (check odbcinst.ini).
Make sure the filepath to configured driver from your INI file (run: odbcinst -j) exist and has read and executable permission flags (O_RDONLY|O_CLOEXEC).
If you still got file not found error, despite the file exists, the problem could be related to libgcc version mismatch as per nehaljwani's GitHub comment. The solution is to update your libgcc by running conda update libgcc command.
Related: ODBC Driver 13 for SQL Server can't open lib on pyodbc while connecting on AWS E2 ubuntu instance.
For macOS, see: Installing ODBC via HomeBrew.
Maybe it is a bit late, but I leave this scripts that worked for me.
My problem was the same as yours and I tested all the options such as changing the driver location, making a symbolic link, modify /etc/*.ini files, etc... nothing worked.
My problem, running python 3.6, pyodbc package in a docker container from alpine was the library libssl1.0.0
Here you will find my installation script for pyodbc Debian 8 (alpine) docker image using the driver v13
DRIVER={ODBC Driver 13 for SQL Server}
The command I run for database connection was:
import pyodbc
connection_string = 'DRIVER={ODBC Driver 13 for SQL Server};'
connection_string += 'SERVER={0};DATABASE={1};UID={2};PWD={3};'.format(host,dbname,user,pwd)
connection = pyodbc.connect(connection_string)
I solve this problem after installing libssl1.0.0.
First, I setup my connection string in this way:
cnxn = pyodbc.connect('DRIVER={/usr/local/lib/libmsodbcsql.13.dylib};
SERVER=myServerIP,1433;DATABASE=myDBName;UID=sa;PWD=dbPassword')
Then, I installed libssl1.0.0:
echo "deb http://security.debian.org/debian-security jessie/updates main" >> /etc/apt/sources.list
apt-get install libssl1.0.0
Finnaly, I setup the locales:
apt-get -y install locales
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
After doing these steps, my python module was able to find and connect to database.
had the same issue once..
1.try conda update libgcc(this is because pyodbc installed through pip and conda look for different versions of the file..)..this might have been fixed .....
link:https://github.com/ContinuumIO/anaconda-issues/issues/1639
look for nehaljwani answer .
2.also check the version number of the odbc file correctly in /etc/odbcinst.ini and /etc/odbc.ini ...names should match and also the driver path.
Had the same problem on a Mac mini M1 with macOS Mavericks. After installing the driver 18 from Microsoft which supports ARM it still did not work.
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18
However isql on the commandline was able to connect to the database.
isql -v -k "DRIVER={ODBC Driver 18 for SQL Server};SERVER=<MYSERVERNAME>;PORT=<MYPORT>;DATABASE=<MYDATABASE>;UID=<USERNAME>;PWD=<PASSWORD>"
Finally what did the trick was to uninstall pyodbc and install it again.
python -m pip uninstall pyodbc
python -m pip install pyodbc

Macosx yosemite installing mysql-python config error

I am trying install mysql-python but always throw error. The command is "pip install mysql-python". I searched in google and tried everything but i am still getting error.
I am using mamp, MAMP's path is "/Applications/MAMP". I tested mysql with php and there is no error, mysql is working. I also connected to mysql by navicat. I am sure that mysql is installed and it is working.
I put the error screenshoot.
You need libmysqlclient.so library to be able to install MySQLdb,
Which come with MySQL Server and client and can also be downloaded with MySQL Connector/C.
Locate the library and set your DYLD_LIBRARY_PATH to have the path where libmysqlclient.so is present.

cx_Oracle.so: undefined symbol:PyUnicodeUCS2_AsEncodedString

I am having issiues installing cx_oracle. I have installed oracle instantclient and cx_oracle oracle packages ones installed i am getting this error while importing cx_oracle. i am running ubuntu 11.10 as host.
import cx_Oracle
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: /usr/lib/python2.7/dist-packages/cx_Oracle.so: undefined symbol:PyUnicodeUCS2_AsEncodedString
any one have any idea how to resolve this issue
cheers
Most probably your Python install uses another unicode format (ucs4) and cx_oracle was compiled with ucs2.
You can install cx_Oracle 5.0.4 with the unicode flag. That worked for me but there is some bug: strange Oracle error: "invalid format text"
Or compile the latest cx_oracle yourself.
http://mrpolo.com.ve/?p=178 (its some language i don't know but it helped)
I addition to #froZieglers answer. When I came along the cx_Oracle page there was no "...Unicode..."-Variant to download anymore. Luckily compiling it myself from source was not a big a hassle then I expected.
Here a summary about what I did (Ubuntu 12.04 LTS, 64bit):
install the proper Oracle XE client rpm with alien (11g, 64bit, etc...)
it installs th /u01/..., I had to adjust .profile too, of course.
download cx_Oracle source-tar, untar, cd into
I did the ln -s command on the so-lib on Oracle, as said in BUILD text file
Install Python headers with sudo aptitude install python-dev
Compile with python setup.py build
Install with sudo python setup.by install
First try failed with distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation
patched setup.py with setting userOracleHome = "/u01/app/oracle/product/11.2.0/xe" after os.getenv("ORACLE_HOME")
sudo python setup.by install then worked
Check with python -c 'import cx_Oracle' succeeded.

Categories