Using freetds and unixODBC to run queries with accents (á,é,í,ó,ú) on SQL Server - sql-server

I am using Ubuntu 14.04 with freetds and unixODBC to connect to a 2008 Microsoft SQL Server. I tested the connection with tsql and isql and it worked correctly. While using isql -v if I include accents (á,é,í,ó,ú) in the query I receive the following response:
[37000][unixODBC][FreeTDS][SQL Server]Incorrect syntax near '?'.
[ISQL]ERROR: Could not SQLExecute
While installing freetds and unixODBC packages I received a warning message saying that special characters would not be accepted unless an extra package was but I can't which one it was. To try and solve this problem I uninstalled freetds and unixODBC, reinstalled them and did not receive any warning message but I am still receiving the same error message when including accents in the queries.
PS: This is the first question I make so sorry if it is vague or incomplete.

After trying for a long while the solution I found was quite simple and worked with tsql and isql. On the data source file for unixODBC I needed to set TDS_Version=8.0.
The file before:
[MSSQL]
Driver = FreeTDS
Server = XXX.XXX.XXX.XXX
Port = XXXX
Database = name
The new file:
[MSSQL]
Driver = FreeTDS
Server = XXX.XXX.XXX.XXX
Port = XXXX
TDS_Version = 8.0
Database = name

Related

Connect to SQL Server database from RHEL with Windows Authentication

I have been working for two weeks in the installation of Superset (from Airbnb) for data visualization on a virtual RHEL machine and the connection with a SQL Server database. But I still cannot connect to this database because of a problem of driver I guess. I tried many things and I would like to know if you have a solution, about a driver I need, about modifications in my configuration etc...
Someone told me about jTDS driver. Maybe I need something like this but for python. If you have any idea, here is what I already did.
1) I tried to connect to the database from Superset :
SQL Alchemy URI : mssql://user:password#fr0-iacls-190.eu.company.corp:10001/dbname
ERROR : {"error": "Connection failed!\n\n
The error message returned was:\n(pyodbc.Error) ('IM002', '[IM002] [unixODBC][DriverManager]
Data source name not found, and no default driver specidied (0) (SQLDriverConnect)')"}
2) I tried almost the same with mssql+pymssql :
SQL Alchemy URI : mssql+pymssql://user:password#fr0-iacls-190.eu.company.corp:10001/dbname
ERROR:{"error":"Connection failed!\n\n
The error message returned was:\n(pymssql.OperationalError) (18456, 'DB-Lib error message 20018,
severity 14:\\nGeneral SQL Server error: Check messages from the SQL Server\\n
DB-Lib error message 20002, severity 9:\\n Adaptive Server connection failed (fr0-iacls-190.eu.company.corp:10001)\\n')"}
3) I tried to connect to the database from my terminal on virtual RHEL machine :
# tsql -S fr0-iacls-190.eu.company.corp -U user
Password:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
20^C
Here I have a timer that increase the number every second. I stopped the example after 20 seconds.
4) Finally I tried a python script like this one :
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=fr0-iacls-190.eu.company.corp;DATABASE=dbname;UID=;PWD=password')
The empty UID is a tip used in another StackOverflow post.
# python connect.py
Traceback (most recent call last):
File "connect.py", line 2, in <module>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=fr0-iacls-190.eu.company.corp;DATABASE=dbname;UID=;PWD=password')
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
To finish, I read about two files, odbc.ini and odbcinst.ini but I don't know if they are in the good directory (/etc). I am not working on root (~) but in in the parent directory of root (cd ~/..)...
Here are the two files if necessary :
odbc.ini
;
; odbc.ini
;
[ODBC Data Sources]
JDBC = Sybase JDBC Server
[JDBC]
Driver = /usr/local/lib/libtdsodbc.so
Description = Sybase JDBC Server
Trace = No
Servername = JDBC
Database = pubs2
UID = guest
[Default]
Driver = /usr/local/lib/libtdsodbc.so
odbcinst.ini
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbcw.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbcw.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1
[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
[MSSQLTest]
Driver = ODBC Driver 13 for SQL Server
Server = [http:]fr0-iacls-190.eu.company.corp[,10001]
#
# Note:
# Port is not a valid keyword in the ~/.odbc.ini file
# for the Microsoft ODBC driver on Linux
#
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.4.0
UsageCount=1
Many thanks for your time and your help.
A few things to try:
Correct the python connection string:
You have the aliased the MS SQL driver in /etc/odbcinst.ini to [ODBC Driver 13 for SQL Server] therefore in your python code you should be using that, rather than SQL Server:
import pyodbc
cnxn = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=fr0-iacls-190.eu.company.corp;Port=10001;Database=dbname;UID=user;PWD=password')
replacing user and password with correct credentials.
Use isql to test a connection using odbc.ini:
Generally you use the odbcinst.ini to setup driver configuration, and then odbc.ini for database instances (referencing the drivers), thus a valid entry to your odbc.ini could be:
[friendly_database_name]
Description=A description to help you remember what this connection is for
Server=fr0-iacls-190.eu.company.corp
Port=10001
Database=dbname
UID=user
PWD=password
If you have isql installed (comes as part of the unixODBC package if not), then you can test with:
$ isql -3 -v friendly_database_name
Ensure there's not a firewall blocking you:
I'm not sure why the tsql command is failing for you, it should return the 1> prompt. Ensure that you can establish a telnet connection to the database server:
$ telnet fr0-iacls-190.eu.company.corp 10001
which should give you something like:
Trying 12.34.56.78...
Connected to fr0-iacls-190.eu.company.corp.
Escape character is '^]'
and then to ctrl + ] and type quit to exit (or do ctrl + c to cancel if the telnet test fails).

Connect to a SQL Server database through RODBC

My question is related to
Trying to connect to an ODBC server using RODBC in ubuntu
and
How to specify include and lib directories when locally installing RODBC?
but I could not find suitable answers to my case.
I want to connect to a SQL Server database on a remote server using RODBC.
I have installed unixodbc and freetds, and can connect in the terminal with T-SQL, so the connection exists.
But when trying to connect in R with (all sensitive info have been replaced by ***):
odbcConnect(dsn="TESTSQL", uid=***, pwd=***)
I get:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=TESTSQL;UID=***;PWD=***") : [RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib '/usr/local/Cellar/freetds/0.95.18/lib/libtdsodbc.so' : file not found
2: In RODBC::odbcDriverConnect("DSN=TESTSQL;UID=***;PWD=***") :
ODBC connection failed
The odbc.ini file being:
[ODBC Data Sources]
TESTSQL = Test database
[TESTSQL]
Driver = MSSQL
Servername = ***.**.**.**
Port = **
Database = ****
TDS_Version = 8.0
I had installed the latest version of freetds, that is 1.00.27, I am hence surprised that this library libtdsodbc.so is missing.
Is that normal? Would you recommend to install the version 0.95.18 or rather stay with 1.00.27 and look for that missing library?
I had to remove freetds:
brew remove freetds
then resintalling it, specifying --with-unixodbc to have the libtsdodbc.so created:
brew install freetds --with-unixodbc
In the odbc.ini, I had then to take care not to confuse "Server" and "Servername", and link the driver to the libtdsodbc.so, so that my odbc.ini looks like:
[ODBC Data Sources]
TESTSQL = Test database
[TESTSQL]
Driver = /usr/local/lib/libtdsodbc.so
Server = ***.**.**.**
Port = **
Database = ****
TDS_Version = 8.0
and connected using the RODBC package
ch1 <- odbcConnect(dsn="TESTSQL", uid=***, pwd=***)
> ch1
RODBC Connection 5
Details:
case=nochange
DSN=TESTSQL
UID=****
PWD=******
it works!
Further detailed informations from this page
http://eriqande.github.io/2014/12/19/setting-up-rodbc.html

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.

RODBC MS SQL access from Ubuntu using FreeTDS

I'm trying to access MS SQL server hosted on my Windows box from an Ubuntu machine. I have a shiny app that access MSSQL that works fine on Windows. I want to host this with shiny server on Ubuntu so that others can access the webpage and provide their SQL server as input to the app.
All of this works fine on Windows. Now I'm not able to get unixODBC working. I think I've got the odbc/freeTDS installation and configuration correct. I'm able to connect and query SQL DB instance from Ubuntu terminal using tsql -S . It took some googling but eventually it worked.
Now, when I try to connect from R I get this error.
sql <- odbcConnect("abc.xyz.com", "uname", "passwd")
In odbcDriverConnect("DSN=abc.xyz.com;UID=uname;PWD=passwd")
: [RODBC] ERROR: state 08001, code 0, message
[unixODBC][FreeTDS][SQL Server]Unable to connect to data source 2: In
odbcDriverConnect("DSN=abc.xyz.com;UID=uname;PWD=passwd")
: [RODBC] ERROR: state 01000, code 20002, message
[unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed 3: In
odbcDriverConnect("DSN=abc.xyz.com;UID=uname;PWD=passwd")
: [RODBC] ERROR: state 01000, code 20017, message
[unixODBC][FreeTDS][SQL Server]Unexpected EOF from the server 4: In
odbcDriverConnect("DSN=abc.xyz.com;UID=uname;PWD=passwd")
: ODBC connection failed
freeTDS.conf
[abc.xyz.com]
host = abc.xyz.com
port = 49475
tds version = 8.0
odbcinst.ini
[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
odbc.ini
[abc.xyz.com]
Description = Shiny testing
Driver = FreeTDS
Trace = No
Server = abc.xyz.com\instance_name
Database = dbanme
port = 49475
This error "Unexpected EOF from the server" is not new to me. I got the same error when I was trying to connect using tsql. I was able to overcome this by adding "tds version = 8.0" line to freeTDS.conf. Not sure how I can get RODBC to use this config. I've read posts were other users were able to sql-server using freeTDS. Not sure what is missing here. I also tried re-installing RODBC.
I fixed this. TDS version was needed in odbc.ini file.
TDS_Version = 8.0
tsql reads version info from freeTDS.conf and worked. isql was failing with same error and it was also looking for this config in odbc.ini. So test with isql if you are configuring for R/Python.
I don't know what is causing the error on your side. Hope this helps:
These are my settings for RODBC 1.3-10 to connect to MSSQL Server 2012. I am also on ubuntu and this works for me.
I think I installed the RODBC package from the Ubuntu repos:
apt-cache policy r-cran-rodbc
r-cran-rodbc:
Installed: 1.3-10-1
Candidate: 1.3-10-1
Version table:
*** 1.3-10-1 0
500 ftp://ftp.fu-berlin.de/linux/ubuntu/ trusty/universe amd64 Packages
100 /var/lib/dpkg/statu
uname -a
Linux xxx 3.13.0-30-generic #54-Ubuntu SMP Mon Jun 9 22:45:01 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
odbc.ini
[mydsn]
APP = unixodbc
Description = master on vmXX
Driver = TDSdriver
Server = vmXX
Database = master
Port = 1433
#Trace = No
#TraceFile = /var/log/freetds/freetds--odbc.log
TDS Version = 7.2
freetds.conf
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
#dump file = /tmp/freetds.log
#debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
[vmXX]
host = 111.222.333.555
port = 1433
tds version = 7.2
client charset = UTF-8
Output from
ps aux | grep "/R"
knb 56969 ....
lsof -p 56969 | grep -i odbc
R 56969 knb mem REG 8,1 72408 8528592 /usr/lib/x86_64-linux-gnu/libodbcinst.so.1.0.0
R 56969 knb mem REG 8,1 400608 8521896 /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
R 56969 knb mem REG 8,1 419680 8525415 /usr/lib/x86_64-linux-gnu/libodbc.so.1.0.0
R 56969 knb mem REG 8,1 96845 8933205 /usr/local/lib/R/site-library/RODBC/libs/RODBC.so
I had a similar error and realized R was just unable to read my user id correctly as it had a slash in it. I placed my credentials in a text file which solved the problem for me.
cred <- "/myPath/ODBC_cred.txt"
sql <- RODBC::odbcConnect("abc.xyz.com", uid=readLines(cred)[1], pwd=readLines(cred)[2])

FreeTDS problem connecting to SQL Server on Mac (Unexpected EOF from the server)

I have setup FreeTDS to connect to SQL Server database. When I try to open TSQL in terminal on Mac (lion) I get this error:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20017 (severity 9):
Unexpected EOF from the server
OS error 36, "Operation now in progress"
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
Does anybody have a clue what could be causing this?
I think I got it, it's the TDS configured version, I had 5.0, which caused the error
Try running tsql -C to check what version are you using, and then you can invoke tsql overriding the version with:
TDSVER=7.0 tsql -H hostname -p port -U domain\\\username -P password
and it worked!
To make this permenant I found I need to edit the freetds.conf file and set it globally in there
vim /usr/local/freetds/etc/freetds.conf
tds version = 8.0
running tsql -C still showed that the version was 5.0 but connecting now worked every time.
TDS version need to match the correct tds protocol to connect to your db server, see below -
http://www.freetds.org/userguide/choosingtdsprotocol.htm
Choosing a TDS protocol version
***DB SERVER | TDS VERSION ***
Microsoft SQL Server 6.x = 4.2
Sybase System 10 and above = 5.0
Sybase System SQL Anywhere = 5.0
Microsoft SQL Server 7.0 = 7.0
Microsoft SQL Server 2000 = 7.1
Microsoft SQL Server 2005 = 7.2
Microsoft SQL Server 2008 = 7.2
Also missing symlink on libtdsodbc.so in /usr/lib/odbc (for me)
sudo ln /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so libtdsodbc.so
should help
I was having the same problem. In my situation, the username for the Microsoft SQL Server database was in the form, DOMAIN\userid. However, when I typed it on the command line, I think it was interpreting the slash as an escape character. When I tried putting the slash the other direction, it still failed. Finally, I tried putting two slashes in the original direction and it worked. Thus the following worked for me:
tsql -S myhost -U DOMAIN\\userid
I encountered the same error. Then I did the following, and the error went away:
pip install pyodbc
Previously, I had used quotes for the DB server, username, and pwd in the TSQL command. This time, I removed the quotes.

Resources