Connect via ODBC
Open Database Connectivity (ODBC) is a common way to connect to databases across many different programming languages and operating systems. ODBC can be used directly from a program to access the database or can be operated in a Command Line Interface (CLI) mode. This example will describe how to connect using the CLI mode and then provide an example of connecting with ODBC in . For details on using the ODBC driver in code, please see the unixODBC page.
To run this example, unixodbc is required. If unixodbc is not installed on your system, the setup instructions in the ODBC Manual cover detailed options available to setup the ODBC driver on your system.
- On a Mac, unixodbc can be installed using brew install unixodbc
- On most Linux distributions, unixodbc can be installed with apt install unixodbc-dev
Check the location of the default odbcinst.ini and ocbc.ini files:
Create a directory for this example:
Obtain the latest version of the ODBC driver from Support and save it to your client machine in this example folder at ~/odbc-example/libocient_odbc.so.
Using the file path listed for DRIVERS and SYSTEM DATA SOURCES in the prior step, copy these files to the example directory:
Set environment variables so the ODBC driver uses your example file for settings:
If you run odbcinst -j again, you should see your updated settings in place for the file locations.
Next, edit the ODBC ini files to configure how ODBC will connect to Ocient.
This example assumes:
- TLS=UNVERIFIED
- IP Address 10.10.1.1 for the SQL Node
- ODBC Driver and files are in ~/odbc-example
- Standard SQL Port 4050
SQL Nodes are installed by default with a self-signed TLS certificate for encryption by default, but a custom certificate can be present. Depending on the TLS configuration, tls=unverified (default) or tls=on can be the required setting.
Add the following lines to the ~/odbc-example/odbcinst.ini file. This sets up a driver named "OcientDW" for use on your system and defines where ODBC should locate the driver file:
Add the following lines to the ~/odbc-example/odbc.ini file. These configure the Data Set named "Ocient" for use in ODBC based programs:
You can test an ODBC driver connection from the command line or a Python script.
Run the following command with the isql utility that ships with unixodbc to test the ODBC connection replacing the username and password with your configured user details:
Then you will be greeted with a SQL prompt.
Example Response:
Success! Now you are connected and can execute queries based on your user’s permissions. Try running: select * from sys.tables to see what tables are defined.
You can connect to Ocient databases with Python by using the pyodbc module. This module requires Python version 3.7 or later.
To connect using Python, the pyodbc module can be used.
With Python, run the following program to connect and run a simple loop over a table, replacing the username and password with your credentials:
Example Response:
For more details on using the ODBC driver and the CLI program, see the ODBC Manual.