This article is part of a series that includes SQLite, Postgresql, Firebird, Oracle RDBMS, HSQLDB, MariaDB, MongoDB, and Excel. The goal is to set up a self-standing environment for testing an ODBC extension for gawk presented here to be completed. Refer to SQLite for installing the required ODBC Driver Manager.
The test system is a debian v11 (bullseye).
As root, follow the documentation here to get and install sqlserver express for Linux. Here are the needed steps:
# wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - # add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)" # apt-get install -y mssql-server # /opt/mssql/bin/mssql-conf setup
A systemd service was set up and launched:
# systemctl status mssql-server --no-pager ● mssql-server.service - Microsoft SQL Server Database Engine Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2021-08-27 15:22:14 CEST; 15s ago Docs: https://docs.microsoft.com/en-us/sql/linux Main PID: 7795 (sqlservr) Tasks: 120 Memory: 880.5M CPU: 5.983s CGroup: /system.slice/mssql-server.service ├─7795 /opt/mssql/bin/sqlservr └─7817 /opt/mssql/bin/sqlservr
Get and install the ODBC drivers for sqlserver:
# apt install tdsodbc # apt install curl # curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list # apt-get update # apt-get install mssql-tools unixodbc-dev
Check that the device has been added system-wide:
# odbcinst -q -d ... [ODBC Driver 17 for SQL Server] # odbcinst -q -d -n "ODBC Driver 17 for SQL Server" [ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1
As debian, create the sampledb database using the native administration tool sqlcmd:
$ sqlcmd -S localhost -U SA -P admin2021! CREATE DATABASE sampledb go 1> SELECT Name from sys.Databases; 2> go Name ----------- master tempdb model msdb sampledb (5 rows affected)
Populate the sampledb database; statements for table creation for slqserver are available here and the ones to populate the tables here.
Click and save the files to create_tables_mssql.sql respectively populate_tables_mssql.sql.
Execute the above SQL scripts still using the default command-line administration tool, sqlcmd:
$ sqlcmd -S localhost -U SA -P admin2021! -i create_tables_mssql.sql $ sqlcmd -S localhost -U SA -P admin2021! -i populate_tables_mssql.sql
Let’s check the data:
cat - <<eot | sqlcmd -S localhost -U SA -P admin2021! USE sampleDB SELECT c.country_name, c.country_id, l.country_id, l.street_address, l.city FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE c.country_id IN ('US', 'UK', 'CN'); eot Changed database context to 'sampledb'. country_name country_id country_id street_address city ---------------------------------------- ---------- ---------- ---------------------------------------- --------------------- China CN NULL NULL NULL United Kingdom UK UK 8204 Arthur St London United Kingdom UK UK Magdalen Centre, The Oxford Science Park Oxford United States of America US US 2014 Jabberwocky Rd Southlake United States of America US US 2011 Interiors Blvd South San Francisco United States of America US US 2004 Charade Rd Seattle (6 rows affected) SQL>
Configure an ODBC DSN by editing the user’s DSN file:
$ vi ~/.odbc.ini ... [mymssqlserverdb] Driver = ODBC Driver 17 for SQL Server Server = localhost #Port = 1433 User = SA Password = admin2021! Database = sampledb Language = us_english NeedODBCTypesOnly = 1
Check the DSN:
$ odbcinst -q -s ... [mymssqlserverdb] $ odbcinst -q -s -n mymssqlserverdb [mymssqlserverdb] Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1 Server=localhost Port= Database=sampledb User=SA Password=admin2021! Language=us_english NeedODBCTypesOnly=1
Try a connection to the mssql db via ODBC using the ODBC Driver Manager test tool, isql:
$ isql -v mymssqlserverdb SA admin2021! +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> SELECT c.country_name, c.country_id, l.country_id, l.street_address, l.city FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE c.country_id IN ('US', 'UK', 'CN'); Output: +-----------------------------------------+-----------+-----------+-----------------------------------------+---------------+ | country_name | country_id| country_id| street_address | city | +-----------------------------------------+-----------+-----------+-----------------------------------------+---------------+ | China | CN | | | | | United Kingdom | UK | UK | 8204 Arthur St | London | | United Kingdom | UK | UK | Magdalen Centre, The Oxford Science Park| Oxford | | United States of America | US | US | 2014 Jabberwocky Rd | Southlake | | United States of America | US | US | 2011 Interiors Blvd | South San Francisco | | United States of America | US | US | 2004 Charade Rd | Seattle | +-----------------------------------------+-----------+-----------+-----------------------------------------+---------------+ SQLRowCount returns 6 6 rows fetched
Test the DSN with the python module pyodbc:
$ python3 import pyodbc cnxn = pyodbc.connect(DSN='mymssqlserverdb;UID=SA;PWD=admin2021!') cursor = cnxn.cursor() cursor.execute("""SELECT c.country_name, c.country_id, l.country_id, l.street_address, l.city FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE c.country_id IN ('US', 'UK', 'CN')""") row = cursor.fetchone() while row: print (row) row = cursor.fetchone() Output: ('China', 'CN', None, None, None) ('United Kingdom', 'UK', 'UK', '8204 Arthur St', 'London') ('United Kingdom', 'UK', 'UK', 'Magdalen Centre, The Oxford Science Park', 'Oxford') ('United States of America', 'US', 'US', '2014 Jabberwocky Rd', 'Southlake') ('United States of America', 'US', 'US', '2011 Interiors Blvd', 'South San Francisco') ('United States of America', 'US', 'US', '2004 Charade Rd', 'Seattle')
mssqlservr is now fully accessible from any ODBC application under the debian account.
Instructions for the other data sources can be accessed through the following links:
SQLite
Firebird
HSQLDB
MariaDB
PostgreSQL
Oracle
MongoDB
Excel
Cet article Installing the ODBC drivers for Microsoft SQLServer for Linux est apparu en premier sur Blog dbi services.