Last active
September 12, 2022 13:46
-
-
Save mariusdkm/a2ec381e7bc1712c29f0ad0b0490671c to your computer and use it in GitHub Desktop.
Revisions
-
mariusdkm revised this gist
Mar 15, 2022 . 1 changed file with 22 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ pip install pyodbc ## Using pyodbc You now have three ways of including the driver in your python program. Note: When I was trying to connect to a MS SQL Server I had to include the port **Always replace `{var}` with your value** @@ -61,4 +61,24 @@ Then you can use the name you put in as `DSN NAME` in your python program ```py import pyodbc pyodbc.connect('DSN={DSN NAME};UID={username};PWD={password}') ``` ## Other ways of connecting to a MS SQL Server There is also a port on mac ports which is the official ODBC Driver for Microsoft SQL Server named [msodbcsql17](https://ports.macports.org/port/msodbcsql17). But at the time of writing an arm version for M1 macs was not available, so it doesn't work, and always threw an error. It should work though on macs with an intel chip. When installing `msodbcsql17` it automatically creates a config in `/opt/local/etc/odbcinst.ini`, so one should be able to just use it in python using `ODBC Driver 17 for SQL Server` as the driver name: ```py import pyodbc pyodbc.connect('DRIVER=ODBC Driver 17 for SQL Server;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}') ``` When trying this on an M1 chip one will always get the error `[unixODBC][Driver Manager]Can't open lib '/opt/local/lib/libmsodbcsql.17.dylib' : file not found (0) (SQLDriverConnect)` even though that file exists. This is because the file is in the architecture `x86_64` as seen with `lipo -info /opt/local/lib/libmsodbcsql.17.dylib`. Before knowing this I tried forcing pyodbc to use it by installing it with the following command. `LDFLAGS="-L/opt/local/lib -liodbc -liodbcinst" CFLAGS="-I/opt/local/include" pip install pyodbc` This just resulted in the literal crash of the python programm with no error message from python. -
mariusdkm revised this gist
Mar 15, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ I didn't find any tutorial using **mac ports** to install pyodbc on a mac with m1, so here is my way, maybe it helps somebody. My goal was to talk to a MS SQL Server. If you want to use Homebrew instead or more information please look here https://gist.github.com/Bouke/10454272 ## Installing pyodbc -
mariusdkm created this gist
Mar 15, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ I didn't find any tutorial using mac ports to install pyodbc on a mac with m1, so here is my way, maybe it helps somebody. My goal was to talk to a MS SQL Server. I got a lot of my information from this gist https://gist.github.com/Bouke/10454272 ## Installing pyodbc The only dependency you are going to need is `freetds`, it will install automatically it's dependencies. **BUT** because we later on need the shared object file `libtdsodbc.so` you need to install it with as the variant `+odbc`. ```bash sudo port install freetds +odbc ``` If everything went smoothly you should now have the following file `/opt/local/lib/libtdsodbc.so` Next you can already install `pyodbc` ```bash # (in your python enviroment) pip install pyodbc ``` ## Using pyodbc You now have three way of including the driver in your python program Note: When I was trying to connect to a MS SQL Server I had to include the port **Always replace `{var}` with your value** ### Just use the path of the library ```py import pyodbc pyodbc.connect('DRIVER=/opt/local/lib/libtdsodbc.so;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}') ``` ### Using unixODBC Put the following into `/opt/local/etc/odbcinst.ini` ```ini [{CONFIG NAME}] Description = ODBC for FreeTDS Driver = /opt/local/lib/libtdsodbc.so Setup = /opt/local/lib/libtdsodbc.so FileUsage = 1 ``` Then you can use the name you put in as `CONFIG NAME` in your python program ```py import pyodbc pyodbc.connect('DRIVER=CONFIG NAME;SERVER={IP address};PORT=1433;DATABASE={database};UID={username};PWD={password}') ``` ### Configure a DSN (Data source name) * Modifiy `/opt/local/etc/odbcinst.ini` as described in "Using unixODBC" * Modifiy `/opt/local/etc/odbc.ini` to include the following: ```ini [{DSN NAME}] Driver = {CONFIG NAME} Server = {IP address} Port = 1433 # You can also include more variables here ``` Then you can use the name you put in as `DSN NAME` in your python program ```py import pyodbc pyodbc.connect('DSN={DSN NAME};UID={username};PWD={password}') ```