ODBC access to SQL Server from Linux with Python

Introduction

I wonder if there are other uses besides machine learning. .. .. It's a niche. .. While thinking, I will explain how to connect to Microsoft SQL Server from Linux via ODBC. Let's run the test in Python.

Install DataDirect SQL Server ODBC driver in Linux environment

  1. Download the trial version of SQL Server ODBC driver for Linux from the website. ..

  2. After downloading, execute the following Linux command to move the package to a new folder.

mkdir datadirect

mv PROGRESS_DATADIRECT_ODBC_SQLSERVER_LINUX_XX.tar.Z datadirect/

cd datadirect/
  1. Extract the package with the following command.
tar -xvf PROGRESS_DATADIRECT_ODBC_SQLSERVER_LINUX_64.tar.Z
  1. Execute the following command in the terminal to install the KSH interpreter.

For Debian

sudo apt-get install ksh

For CentOS / RHEL

sudo yum install ksh
  1. Execute the following script in the terminal to execute the installation.
./unixmi.ksh
  1. When the prompt is displayed, enter Enter or "Y" to check the OS.

image.png

  1. I agree to the license agreement with "yes". image.png

  2. Enter your name and company name at the prompt. Enter EVAL for the serial number and key. image.png

  3. A screen will appear asking you to install all the included drivers or select one driver to install. Here, select option 2.

image.png

  1. A list of drivers to install will be displayed. Select option 11'SQL Server Wire Protocol'.

image.png

  1. Check the path. If you cannot confirm it, enter the path.

image.png

  1. Wait for a cup of tea until the installation is complete. If you want to install another Linux driver, you can try various things with "Y", but this time, use "N" to exit the installer.

image.png

SQL Server ODBC driver settings

  1. Installation path /Progress/DataDirect/Connect_64_for_ODBC_71 (The end differs depending on the version) Go to and execute the following command in the terminal. Add the environment variables ODBCINI, ODBCINST, LD_LIBRARY_PATH.
sudo chmod +x odbc.sh

./odbc.sh
  1. Check if the variables ODBCINI, ODBCINST and LD_LIBRARY_PATH are installed normally with the following command. You should see the path to the file odbc.ini.
echo $ODBCINI

./odbc.sh
  1. If the environment variables are not added correctly, add these values to ~ / .bashrc.
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
  1. Refresh the terminal settings with the following command.
source ~/.bashrc
  1. Path /install_path/Progress/DataDirect/Connect64_for_ODBC_71/odbc.ini Open the odbc.ini file located in and configure the SQL Server connection details as shown in the following sample odbc.ini.
[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

  1. Save the odbc.ini file and exit.

SQL Server ODBC Connection Test

  1. Let's test the ODBC driver. This time, let's do it with a simple Python script. Run the following command to install the pyodbc package.

pip install pyodbc

If you get the exception that the <sql.h> library cannot be found, you may need to install the unixODBC package. You can install it by executing the following command.

Debain:

sudo apt-get install unixodbc unixodbc-dev

CentOS/Redhat:

sudo apt-get install unixODBC unixODBC-devel
  1. Save the following script in a python file and execute it. Check if you can connect to SQL Server successfully.
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()
  1. After successfully connecting to SQL Server, you can add the following script to the above file to output the table in the database.
##Print TableNames

for row in cursor.tables():

                if row.table_type == "TABLE":

                                print (row.table_name)

                                print (row)
  1. You can also add the following script and execute the script. Notice that there is a Northwind database that you are trying to query.
##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)

This article uses SQL Server Authentication mode, but the DataDirect SQL Server ODBC and JDBC drivers also support Windows Authentication and Kerberos, so take that opportunity.

Recommended Posts

ODBC access to SQL Server from Linux with Python
ODBC connection to FileMaker 11 Server Advanced with Python 3
Data integration from Python app on Linux to Amazon Redshift with ODBC
Yum command to access MySQL with Python 3 on Linux
Get data from database via ODBC with Python (Access)
Create folders from '01' to '12' with python
How to access wikipedia from python
Access the host SQL Server with python27 / pyodbc on the container
[In-Database Python Analysis Tutorial with SQL Server 2017]
Push notification from Python server to Android
sql from python
I made a server with Python socket and ssl and tried to access it from a browser
How to access RDS from Lambda (python)
[Amazon Linux] Switching from Python 2 series to Python 3 series
Data integration from Python app on Windows to Amazon Redshift with ODBC
Send a message from Slack to a Python server
Install Windows 10 from a Linux server with PXE
How to handle Linux commands well from Python
Back up from QNAP to Linux with rsync
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Local server with python
[In-Database Python Analysis Tutorial with SQL Server 2017] Step 2: Import data to SQL Server using PowerShell
How to "cache enabled" access to FPGA memory from Linux
How to scrape image data from flickr with python
From buying a computer to running a program with python
[Linux] [Python] [Pandas] Load Microsoft Access database (* .mdb) with Pandas
Connect to BigQuery with Python
Post from Python to Slack
Cheating from PHP to Python
Post to slack with Python 3
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Access Oracle DB from Python
Switch from python2.7 to python3.6 (centos7)
Switch python to 2.7 with alternatives
Write to csv with Python
Easy HTTP server with Python
Access Google Drive with Python
screen and split screen with python and ssh login to remote server
[Linux] Copy data from Linux to Windows with a shell script
Try to extract Azure SQL Server data table with pyodbc
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Python] Regularly export from CloudWatch Logs to S3 with Lambda
Call Matlab from Python to optimize
[Scp] Copy files locally from Linux server with Tera Term ssh scp
Python: How to use async with
Call C from Python with DragonFFI
Subscript access to python numpy array
Link to get started with python
Device drivers that "cache enabled" access to FPGA memory from Linux
Using Rstan from Python with PypeR
[Python] Write to csv file with Python
Install Python from source with Ansible
Nice to meet you with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Operate DynamoDB from Python like SQL.
Try to extract a character string from an image with Python3
Introduction to Data Analysis with Python P17-P26 [ch02 1.usa.gov data from bit.ly]
Convert list to DataFrame with python