how to setup odbc for MSSQL SERVER correctly - sql-server

i have made a QT Desktop application as an Appimage. On my development laptop it works fine connecting to the database flawless. But when i put the Appimage to my PC where also the MS SQL Server is hosted it does not work anymore.
My Laptop and PC are Linux Mint 19 Laptop is 19.1 and PC is 19.3 to be specific.
I have installed odbc and tds following this guide:
https://help.interfaceware.com/kb/904
Ubuntu
Run the following commands using a terminal window, or the CLI:
Get the latest package info:
sudo apt-get update
Install libodbc.so, libtdsS.so, isql, and isqlinst:
sudo apt-get install unixodbc
Install libtsdsodbc.so and tsql:
sudo apt-get install tdsodbc
Create a file called odbc.ini
i also edited the /etc/odbc.ini
like this:
Description = MS SQL Server Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Server = 192.168.0.10
Port = 1433
but when i run the application on the PC i always get:
QSqlError("0", "QODBC3: Unable to connect", "[unixODBC][Driver Manager]Can't open lib 'libtdsodbc.so' : file not found")
i also checked that the library is really under the specified path:
/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
yes it is really there.
so i asking what do i missing here?
help would be really appreciated
best regards
Rolf

I found the solution myself.
I tired to falsify the odbc.ini and the odbcinst.ini on my laptop trying to
reproduce the error there i found that it did took no effect.
After searching for the libtdsodbc.so on the laptop i found it also located at
/usr/local/lib/
so i copied the library from /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so to
/usr/local/lib
and that solved the issue and the QT application is now working fine.

Related

Python cant find ODBC Driver on Heroku after setting everything

I have gone through every possible solution on the internet, but I'm unable to make pyobdc get the drivers on heroku.
The steps I have used to create the app are as follows:
heroku create
heroku config:set FLASK_CONFIG=heroku
heroku buildpacks:add heroku/python
heroku buildpacks:add --index 1 heroku-community/apt
git push heroku master
I tried even with odbc buildpack but still no luck:
heroku buildpacks:add https://github.com/iFix/heroku-buildpack-odbc.git
After going through microsoft website, I trimmed down my Aptfile to instuct heroku to install the following packages:
# install msodbcsql17
https://packages.microsoft.com/ubuntu/16.04/prod/pool/main/m/msodbcsql17/msodbcsql17_17.4.2.1-1_amd64.deb
# install mssql-tools
https://packages.microsoft.com/ubuntu/16.04/prod/pool/main/m/mssql-tools/mssql-tools_17.4.1.1-1_amd64.deb
# install unixodbc-dev
unixodbc-dev
This makes pyodbc installation go without error. But when I run pyodbc.drivers(), it returns nothing. The same command on Ubuntu 16.04 returns "ODBC Driver 17 for SQL Server"
The source code for the project is at : https://github.com/IamVNIE/odbcTestHeroku
The hosted app is at : https://pyodbctest.herokuapp.com/
Can someone please provide some pointers to make this work.
I have solved this issue by precompiling ODBC Driver 17 for SQL Server on a machine running Ubuntu 18.04 and copying the libmsodbcsql-17.5.so.2.1 and msodbcsqlr17.rll files into appropriate directories via a Heroku buildpack. This is ODBC Driver 17.5 for SQL Server and I likely won't be compiling other versions of this driver, but I imagine the concept remains the same.
The Heroku buildpack and its requirements can be found here https://github.com/matt-bertoncello/python-pyodbc-buildpack.git
Previous answer worked for me until I had to switch to heroku stack 22. After that I started getting the following error:
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')
To solve this issue I switched to another library python pymssql, that handles connections to Microsoft SQL server in azure.
here is a simple example
conn = pymssql.connect(server=xxx,\
user=yyy, \
password=zzz, \
database=xxx)
cursor = conn.cursor(as_dict=True)
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
conn.close()

RODBC on macOS Catalina

I'm having trouble getting RODBC to work on macOS Catalina. I'm trying to connect to MS SQL Server. I have installed unixODBC using Homebrew and installed the Microsoft ODBC drivers from here. RODBC installs fine, and I installed it using this as suggested elsewhere:
install.packages('RODBC', type="source", configure.args='--with-odbc-include=/usr/local/lib' )
But when I run this:
RODBC::odbcDriverConnect(connection = "driver={ODBC Driver 17 for SQL Server};server=XYZ;database=XYZ;UID=XYZ;PWD=XYZ")
it hangs -- I have to cancel the command, and then I get a bunch of warnings that look like this:
[RODBC] ERROR: state IM003, code 0, message [iODBC][Driver Manager]Specified driver could not be loaded
I know that the SQL Server driver is installed, since using the ODBC package in R works -- with the same driver! But for some reason RODBC is using iODBC, and is apparently looking in the wrong place for the driver.
I'm trying to write code that will run on different platforms, and I've successfully used RODBC on Ubuntu and Windows. But I'm having a tough time figuring out how to get RODBC to work on my MacBook.
I had the same issue connecting with Impala on Mac after some upgrades. The workaround was to make sure RODBC used unixodbc instead of iODBC as follows:
install unixodbc. I used brew.
Remove RODBC with remove.packages("RODBC")
Reinstall RODBC from source and specify the path to unixodbc lib and include
with
install.packages("RODBC", type = "source", configure.args = c("--with-odbc-include=/usr/local/include/","--with-odbc-lib=/usr/local/lib/") )
On my computer unixodbc is in /usr/local

PHP 7.x connection with MSSQL server with MAMP

I am trying to connect mssql server to PHP 7.0.8 through MAMP. I have tried using freetds. On some blog people are saying to use pdo_dblib.so extension but it's not working.
Please guide me through the process of connection.
For those who still have this problem:
/Applications/MAMP/bin/php/php7.2.1/bin/pecl install sqlsrv pdo_sqlsrv
Edit php.ini:
extension=sqlsrv.so
extension=pdo_sqlsrv.so
If necessary, use brew install autoconf if you don't have it already.
While the answers posted by Vague Space and Pedro Santiago helped, I still think the answers are a bit lacking and incomplete… Or ask you to do too much. Honestly the official Microsoft instructions are overkill when they state you need to install their Docker image of SQL Server and such? C’mon… Most people just need the drivers installed to make a connection.
So here is my answer based on my experience installing the pdo_sqlsrv.so and sqlsrv.so modules in MAMP (version 5.2) but should work for most any MAMP version that supports some flavor of PHP 7.
Adjust the .bash_profile so MAMP’s binaries and libraries are a part of your $PATH settings.
First, adjust your .bash_profile so the MAMP stuff is in there; makes it easier to launch and work with MAMP specific binaries and ensures MAMP libraries are checked when doing things like installing new modules like this.
The way I like to do it is like this; set $MAMP_BIN and $MAMP_PHP variables like this and then rebuild the $PATH variables:
# MAMP stuff.
export MAMP_BIN="/Applications/MAMP/Library/bin";
export MAMP_PHP="/Applications/MAMP/bin/php/php7.2.10/bin";
# Final $PATH setting.
export PATH="/usr/local/bin:/usr/local/sbin:$MAMP_BIN:$MAMP_PHP:$PATH";
Save it and just log out of the Terminal session and back in, or just resource the .bash_profile like this:
source ~/.bash_profile
With that done, let’s install the core Microsoft ODBC binary stuff.
Install the Microsoft ODBC stuff.
Do this to install the core ODBC stuff on macOS; be sure to have Homebrew installed:
brew tap microsoft/SQLSRV-release https://github.com/Microsoft/homebrew-SQLSRV-release
brew update
brew install --no-sandbox msodbcsql17 SQLSRV-tools
Then when that’s done, go ahead and install the Unix ODBC stuff like this:
brew install unixodbc
Now install the actual PHP modules via PECL:
pecl install sqlsrv pdo_sqlsrv
With the modules installed, add them to the php.ini file in MAMP so PHP can recognize it. For PHP 7.2.10 on MAMP 5.x it should be located here:
/Applications/MAMP/bin/php/php7.2.10/conf/php.ini
And just add these config lines to the bottom of the file:
; Enable 'Microsoft Drivers for PHP for SQL Server' extension module
extension = sqlsrv.so
extension = pdo_sqlsrv.so
; Configuration
;sqlsrv.WarningsReturnAsErrors = 1
;sqlsrv.LogSeverity = 0
;sqlsrv.LogSubsystems = 0
;sqlsrv.ClientBufferMaxKBSize = 10240
;pdo_sqlsrv.log_severity = 0
;pdo_sqlsrv.client_buffer_max_kb_size = 10240
Note, most tutorials—and even PECL when you install the modules—simply mention adding extension = sqlsrv.so and extension = pdo_sqlsrv.so to the php.ini config, but these config options are the ones that RedHat has when installing the PHP SQLSRV via the Remi repo. Yeah, most of them are commented out but I still like having it there for reference.
Follow this guide through step 3: Microsoft PHP drivers for SQL Server
Find where pecl drops extensions in your local machine
Copy the files pdo_sqlsrv.so and sqlsrv.so into your MAMP's PHP extension directory. Mine was located at /Applications/MAMP/bin/php/php7x.x/lib/php/extensions/no-debug-foo-bar
Edit your php.ini file to include the new extensions:
extension=sqlsrv.so
extension=pdo_sqlsrv.so
Restart your MAMP servers.
having just done this in 2019 with MAMPPRO4 on windows 10 (follow upto step 4 to test that you are connected and then do point 9 ) point 5 onwards is for changing the path in the command line
download dll files from microsoft
https://www.microsoft.com/en-gb/download/details.aspx?id=20098
follow the instruction after running the exe file and place the dll
files into the extension directory of the php version that you are
using eg: MAMP/bin/php/php7.1.29/ext
check phpinfo for the Loaded Configuration File of the php.ini file
add the 2 dll files depending on your requirements (I wasted time by
using the 64.dll) make sure you are using ts(thread safe) not
nts(none thread safe) in the file name of the dll
extension=php_sqlsrv_71_ts_x86.dll
extension=php_pdo_sqlsrv_71_ts_x86.dll
in control panel search for advanced system settings and click
click Environment Variables
under system variables not user variables click path and click edit
click new and add C:\MAMP\bin\php\php7.1.29 (Edit this to your path)
restart MAMP
open a new command line an enter php -v
you should see the php version displayed

Connect to SQL Server using SQLAlchemy

I'm trying to connect to a SQL Server Express database using SQLALchemy and pyodbc, but I'm continuously getting the error:
(pyodbc.Error) ('IM002', '[IM002] [unixODBC][Driver Manager]Data
source name not found, and no default driver specified (0)
(SQLDriverConnect)')
And I really don't understand if my engine url is wrong or what else.
My scenario is the following:
I'm on a Mac
I have a docker container (based on a Debian image with unixodbc and unixodbc-dev) in which my python app tries to connect to...
a virtualbox virtual machine running windows 8 with SQL express 2014...
I configured a user for the SQL express, with SQL Server authentication:
user: ar_user
password: ar_psw
...then:
I configured TCP ports as 1433 and disabled dynamic ports (SQL Server Configuration Manager > Network Configurations > Protocols).
I turned off Windows Firewall.
I used an Host-only adapter for the VM running windows8
now...
The VM is accessible from the host (my mac), since a:
ping -c 3 vm-ip
succeed!
But although I tried every possible permutation of user, password, ip, server name and port:
'mssql+pyodbc://ar_user:ar_psw#vm-ip/master'
'mssql+pyodbc://ar_user:ar_psw#vm-ip:1433/master'
'mssql+pyodbc://IE10WIN8\\SQLEXPRESS'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS:1433'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS:1433/master'
...and many more!
I always get the "datasource not found error".
What should I do?
ps: the vm is pingable even in the docker container!
UPDATE (solved but not 100%):
I solved in this way:
I configured FreeTDS driver using /etc/odbcinst.ini in this way:
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
client charset = UTF-8
and in /etc/freetds/freetds.conf:
[global]
tds version = 7.3
client charset = UTF-8
Then I created the engine using the following string:
'mssql+pyodbc://my_user:my_psw#machine_ip:1433/my_db?driver=FreeTDS'
It seems to work properly, but I get this warning:
SAWarning: Unrecognized server version info '95.12.255'. Version
specific behaviors may not function properly. If using ODBC with
FreeTDS, ensure TDS_VERSION 7.0 through 7.3, not 4.2, is configured in
the FreeTDS configuration.
I also defined the TDS version using environment variables but it doesn't fix the issue... any idea?
I wrote a tutorial here of how to do this. Essentially, you need to:
brew install unixodbc
brew install freetds --with-unixodbc
Add the freetds driver to odbcinst.ini
Add a DSN (Domain Source Name) to odbc.ini named "MY_DSN"
pip install pyodbc
e = create_engine("mssql+pyodbc://username:password#MY_DSN")
The walkthrough here does a much more thorough job of explaining this, including issues with SQL Server/FreeTDS Protocol Version Compatibility.

iODBC does not work under Mac OSX 10.6.4

I've installed iODBC over the package (http://www.iodbc.org/dataspace/iodbc/wiki/iODBC/) and set up all config files as described here: http://blog.opensteam.net/past/2009/1/28/rails_ms_sql_on_mac/
I set up the same thing on a Linux machine and it worked fine. On my Mac OSX 10.6.4, I get this error if i test the connection:
xxx#xxx:/opt/local/include$ iodbctest "dsn=res;uid=user;pwd=pass"
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0709.0909
1: SQLDriverConnect = [iODBC][Driver Manager]dlopen(/opt/local/lib/libtdsodbc.so, 6): Symbol not found: _CreateDataSource
Referenced from: /usr/lib/libiodbcinst.2.dylib
Expected in: flat namespace
(0) SQLSTATE=00000
2: SQLDriverConnect = [iODBC][Driver Manager]Specified driver could not be loaded (0) SQLSTATE=IM003
I have no idea how to debug this :(
It would be interesting to see what params you have in your odbc.ini and odbcinst.ini files for the DSN and FreeTDS driver being used ?
Looks as if the Apple iODBC Driver Manager is not able to load setup routines required for the driver nor does the FreeTDS driver itself.
The OpenLink iODBC Driver Manager for Mac OS X (which Apples bundled one is based on) is Framework based and does include routines for loading generic setup and login dialogs for those ODBC drivers that do not have built in one. You can download it free (as it is open source) from the following location:
http://www.iodbc.org/downloads/iODBC/iodbc-sdk-3.52.7-macosx-10.5.dmg (it is for 10.5 and 10.6)
Steps to follow:
brew install freetds
subl ~/Library/ODBC/odbc.ini
[sqlserver01]
Driver=/usr/local/lib/libtdsodbc.so
TDS_Version=7.2
Server=192.168.8.7
Port = 1433
Trace = Yes
Description=ds01
# Database=
# can't specify username and password for freetds
subl ~/.freetds.conf
host = ds01.uswa.net # server name
port = 1433
tds version = 8.0 # works with 2008+
sudo pip install pyodbc
sudo pip install sqlsoup
The Python Connection String would look like this:
# Steve is my username; steve is my password; #sqlserver01 is my DSN in odbc.ini
db = sqlsoup.SQLSoup('mssql+pyodbc://Steve:steve#sqlserver01')
The SQLAlchemy way would look like this:
engine = create_engine('mssql+pyodbc://Steve:steve#sqlserver01')`
Do whatever queries…
I had the same problem, linux works, Lion 10.7 not (I guess same problem for 10.6).
I'm using FreeTDS driver to connect, compiling from the source code: it looks like that when compiling, libtdsodbc.so is compiled for the wrong architecture or 32bit/64bit.
I followed this guide (excluding Excel part), and everything worked like a charm.
http://asmiler.blogspot.fr/2011/10/accessing-mssql-databases-from-excel.html

Resources