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.
Download the trial version of SQL Server ODBC driver for Linux from the website. ..
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/
tar -xvf PROGRESS_DATADIRECT_ODBC_SQLSERVER_LINUX_64.tar.Z
For Debian
sudo apt-get install ksh
For CentOS / RHEL
sudo yum install ksh
./unixmi.ksh
I agree to the license agreement with "yes".
Enter your name and company name at the prompt. Enter EVAL for the serial number and key.
A screen will appear asking you to install all the included drivers or select one driver to install. Here, select option 2.
sudo chmod +x odbc.sh
./odbc.sh
echo $ODBCINI
./odbc.sh
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
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
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)
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