This tutorial walks you through on how you can connect to SQL Server database from Linux machine using an ODBC driver. We drafted a step-by-step guide to:
1. install SQL Server ODBC Driver on a Linux machine
2. Configure the ODBC driver
3. Test the ODBC connection
mkdir datadirect
mv PROGRESS_DATADIRECT_ODBC_SQLSERVER_LINUX_XX.tar.Z datadirect/
cd datadirect/
tar -xvf PROGRESS_DATADIRECT_ODBC_SQLSERVER_LINUX_64.tar.Z
Debian:
sudo apt-get install ksh
CentOS/RHEL:
sudo yum install ksh
./unixmi.ksh
sudo chmod +x odbc.sh
./odbc.sh
echo $ODBCINI
export LD_LIBRARY_PATH=/install_path/Progress/DataDirect/Connect64_for_ODBC_71/lib
export ODBCINI=/ install_path/Progress/DataDirect/Connect64_for_ODBC_71/odbc.ini
export ODBCINST=/ install_path/Progress/DataDirect/Connect64_for_ODBC_71/odbcinst.ini
source ~/.bashrc
[ODBC Data Sources]
SQL Server Legacy Wire Protocol=DataDirect 7.1 SQL Server Legacy Wire Protocol
[ODBC]
IANAAppCodePage=4
InstallDir=/home/progress/Progress/DataDirect/Connect64_for_ODBC_71
Trace=0
TraceFile=odbctrace.out
TraceDll=/home/progress/Progress/DataDirect/Connect64_for_ODBC_71/lib/ddtrc27.so
[SQL Server Wire Protocol]
Driver=/home/progress/Progress/DataDirect/Connect64_for_ODBC_71/lib/ddsqls27.so
Description=DataDirect 7.1 SQL Server Wire Protocol
AlternateServers=
AlwaysReportTriggerResults=0
AnsiNPW=1
ApplicationName=
ApplicationUsingThreads=1
AuthenticationMethod=1
BulkBinaryThreshold=32
BulkCharacterThreshold=-1
BulkLoadBatchSize=1024
BulkLoadFieldDelimiter=
BulkLoadOptions=2
BulkLoadRecordDelimiter=
ConnectionReset=0
ConnectionRetryCount=0
ConnectionRetryDelay=3
Database=<database_name>
EnableBulkLoad=0
EnableQuotedIdentifiers=0
EncryptionMethod=0
FailoverGranularity=0
FailoverMode=0
FailoverPreconnect=0
FetchTSWTZasTimestamp=0
FetchTWFSasTime=1
GSSClient=native
HostName=<server_name>
HostNameInCertificate=
InitializationString=
Language=
LoadBalanceTimeout=0
LoadBalancing=0
LoginTimeout=15
LogonID=<logon id> (optional)
MaxPoolSize=100
MinPoolSize=0
PacketSize=-1
Password=<password> (optional)
Pooling=0
PortNumber=1433
QueryTimeout=0
ReportCodePageConversionErrors=0
SnapshotSerializable=0
TrustStore=
TrustStorePassword=
ValidateServerCertificate=1
WorkStationID=
XMLDescribeType=-10
pip install pyodbc
Tip: If you get an exception saying that it cannot find <sql.h> library, you might have to install unixODBC packages. You can install them by running the following command.
Debain:
sudo apt-get install unixodbc unixodbc-dev
CentOS/Redhat:
sudo apt-get install unixODBC unixODBC-devel
import pyodbc
##connecting to your database through ODBC
cnxn = pyodbc.connect('DSN=SQL Server Wire Protocol;UID=sa;PWD=pass', autocommit=True)
cursor = cnxn.cursor()
##Print TableNames
for row in cursor.tables():
if row.table_type == "TABLE":
print (row.table_name)
print (row)
##Executing a simple query and printing the records
cursor.execute("SELECT EmployeeID, LastName, FirstName, Address, City, Region from Northwind.dbo.Employees")
for row in cursor.fetchall():
print (row)
We hope this tutorial helped you to connect to your SQL Server database. Note that we have used the SQL Server Authentication mode for this tutorial, but DataDirect SQL Server ODBC and JDBC drivers also support Windows authentication and Kerberos too.
Also please note that we recommend and require use of the Progress ODBC driver manager, which is shipped with the Progress ODBC drivers. Progress DataDirect ODBC drivers are not certified with the unixODBC Driver Manager and reported issues must be reproducible with a supported version of the DataDirect ODBC Driver Manager for Unix/Linux.
Feel free to try the SQL Server ODBC driver for 30 days and contact us if you need an extension for the trial.