How to interact with mssql from python? - sql-server

How to interact with mssql from python? I realy nead it to write my site.
I tried pyodbc
(
pip install pyodbc
)
but cmd says there is an error in setup.py.
Pleese, help me!
P.S. I am using windows and mssql.

[Updated Answer]
Follow these steps to install pyodbc and pymssql in 64-bit Windows with *Python 3.**
Update pip tool: python -m pip install pip -U
Update python setuptools : python -m pip install setuptools -U
Install pyodb : pip3 install pyodbc -U
Download and install MS Visual C++ 14.0 Build Tools. It's required for installing pymssql.
double click on downloaded file and select Workloads → Visual C++ build tools then in optional features tab select Windows 10 SDK(windows 8 user select Windows 8 SDK) and install.
Install pymssql: pip3 install pymssql -U
[Old Answer]
One can also try these unofficial wheel files, if above method doesn't work.
You can install the same using the unofficial pyodbc wheel for python 3.* from here, just download the appropriate wheel (*.whl) file form this link and install it using
pip install **downloaded_wheel_file_name**.whl
Note: Don't forget to change the downloaded_wheel_file_name with your downloaded wheel file's name.

You need to have Microsoft ODBC driver for sql in Windows to make
pip install pyodbc
work.
Try to follow this link and install required drivers.

Try this #Kostya2007: pip install --allow-external pyodbc --allow-unverified pyodbc pyodbc
or
pip install https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/pyodbc/pyodbc-3.0.7.zip
depending on your Python version
or
easy_install pyodbc-3.0.7.win32-py2.7.exe

Related

PECL installation conflict isssue

I am trying to install Pear package using following command
sudo yum install php-dev php-pear
But it's giving me following error:
Transaction check error:
file /usr/bin/php from install of php-cli-5.4.16-48.el7.x86_64 conflicts with file from package ea-php-cli-1.0.0-9.13.1.cpanel.x86_64
I am installing this package for sqlsrv driver but I am not able to do.
It seems you using WHM/cpanel right? Don't install extensions using CLI. You can install it using PECL installer in WHM dashboard.
here the screenshot of PECL module installer
But if the module is not available on the GUI installer, you can install the module using built-in PHP from the cPanel. the path of pecl executable is /usr/bin/ea-phpVERSION-pecl.
an example if you want to install sqlsrv on php 7.3:
/usr/bin/ea-php73-pecl install pdo_sqlsrv
reference: https://support.cpanel.net/hc/en-us/articles/360053132353-How-install-pdo-sqlsrv-to-connect-to-MSSQL-Servers

How to properly install pyodbc and drivers on M1 mac

I have a new M1 pro macbook and I'm trying to install pyodbc and relevant drivers on my machine.
So far I have installed unixodbc via homebrew and ODBC drivers according to the instructions found here: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15
I then pip installed pyodbc but when I try to run pyodbc.connect() within a test script, python just aborts with the following message, regardless which connection string I use:
[1] 2086 abort python test.py
After investigating a bit more I find that pyodbc doesn't have any drivers. When I run pyodbc.drivers() I get an empty list. So it seems like pyodbc cannot find the drivers I installed earlier.
Here is the output when I run odbcinst -j:
unixODBC 2.3.9
DRIVERS............: /opt/homebrew/etc/odbcinst.ini
SYSTEM DATA SOURCES: /opt/homebrew/etc/odbc.ini
FILE DATA SOURCES..: /opt/homebrew/etc/ODBCDataSources
USER DATA SOURCES..: /Users/kdot/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
and the odbcinst.ini file contains:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=3
I've also tried to explicitly give the driver file path to pyodbc in the connection string but then pyodbc.connect() just hangs instead of aborting right away.
I am running all of this within a virtual environment with the following versions:
python: 3.8.10
pyodbc: 4.0.32
Does anyone know how to make pyodbc see and use the relevant drivers?
First, install the ODBC driver as instructed by Microsoft here.
Further, Microsoft recommends creating symbolic links as follows:
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
However, the location of those files on M1 macs might be different, depending on the the default homebrew directory. In my case, the following paths worked:
sudo ln -s /opt/homebrew/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /opt/homebrew/etc/odbc.ini /etc/odbc.ini
This is what worked for me on a m2 macbook (from the official ms documentation).
Make sure you have homebrew installed.
/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
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
sudo ln -s /opt/homebrew/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /opt/homebrew/etc/odbc.ini /etc/odbc.ini
The problem here is that "several critical CLI tools like nvm and brew do not have native versions built for the new M1 architecture." Source.
Luckily Apple Silicon comes with a translation layer named Rosetta 2, which is basically a CLI interface through which you have to install tools like brew in order for them to install correctly.
In this Medium post you can read how to use Rosetta 2. We got pyodbc working by installing Python 3.9, pip, wheel, openssl, unixodbc, freetds, the correct drivers and finally pyodbc (in this order) all with Rosetta 2.
Found the following link helpful to install pyodbc on MacBook M1 Chip.
We happen to get sql.h file missing error and if we try to install the wheel directly from the site it throws platform not supported error.
Follow the below document and then add the export commands into your zrc or bash file and you should be good to go.
https://whodeenie.medium.com/installing-pyodbc-and-unixodbc-for-apple-silicon-8e238ed7f216
Need more reading about the bug and how people tried to learn it.
https://github.com/mkleehammer/pyodbc/issues/846
I still had this issue with the default Homebrew & ODBC installations on macOS Ventura with a 2022 MacBook Pro.
The only thing that worked was to completely uninstall Homebrew and the ODBC drivers and then reinstall both with the prefix arch -x86_64.
$ arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ arch -x86_64 brew install msodbcsql18 mssql-tools18

How to connect puckel/docker-airflow to local windows SQL Server?

So, I'm trying to connect to my local instance of SQL Server on windows from airflow dag.
The major error is that pyodbc or pymmssql libraries are not installed even after explicitly mentioning them in the docker file. I've followed the instructions from Microsoft ODBC Driver for SQL Server under Debian 9 and Medium as the image is built on it.
All the tasks in dag run fine except for the one I am trying to connect to the SQL Server with the error:
[2020-04-03 22:23:42,303] {{taskinstance.py:1128}} ERROR - No module
named 'pyodbc'
If I try pip install from the airflow container it is not installing in the /usr/lib/python3.7/site-packages and not able to import in the dag.
I have also included requirements.txt in the volume but had no luck.
Did anyone had a successful connection in this scenario?
I was able to solve this issue by building the airflow image form the Dockerfile. while building you need to add few dependencies.
sqlalchemy:
&& pip3 uninstall SQLAlchemy \
&& pip3 install SQLAlchemy==1.3.15 \
pyodbc:
&& apt-get install python3-dev -y\
&& apt-get install unixodbc-dev -y\
&& pip3 install pyodbc \
Another long way would be to change the USER as root and build the image and ap-get install from the container and change change the USER back to airflow but this is more efficient if you know what you are installing.

Installing pyodbc for Python 3.3 on windows (can't use pip)?

I'm trying to connect to the Microsoft SQL server.
I'm using python code to do so, and I want to download pyodbc.
I go to the Python33 directory by doing this: cd C:\Python33\, and I try typing in pip install pyodbc, but it doesn't recognize pip.
Did anyone else have this problem with Python 3.3?
I'm also trying to do this:
C:\Python34\Scripts>pip install pyodbc
But it says: "The system cannot find the path specified."
Pip is a stand-alone tool, you will have to install that as well in 3.3. Here is a link on a really good explanation:
(How do I install pip on Windows?)
In 3.4, pip was included, but you will have to call it with
python -m pip
inside your scripts directory

Install rst2man failed for varnish agent [fix]

I'm trying install varnish agent on ubuntu server using virtual box. There is a message in the middle installation process that say "you need rst2man installed to make dist".
When I tried install rst2man using "sudo apt-get install rst2man" another message come in :
Reading package list....done
Building dependecy tree
Reading state information....done
E: unable to locate package rst2man
"unable to locate package rst2man" what does it mean ? anyone know ? please help.
Problem fix:
I tried to install python-docutils for the rst2man.
rst2man is a virtual package, then you need install python-docutils package than provide rst2man and utilities for the documentation of Python modules.
sudo apt-get install python-docutils
After install this package, try the command:
sudo which rst2man
python-docutils wasn't working for me
I had to install the package docutils-common
sudo apt-get install docutils-common
In case if you installing varnish-agent-2 - after
sudo apt-get install python-docutils
you should execute again
./configure
Working on CentOS and likely RHEL as well:
yum install python-docutils

Resources