Using the CData ODBC Driver for Lakebase in PyCharm

Jerod Johnson
Jerod Johnson
Senior Technology Evangelist
Connect to Lakebase as an ODBC data source in PyCharm using the CData ODBC Driver for Lakebase.

The CData ODBC Drivers can be used in any environment that supports loading an ODBC Driver. In this tutorial we will explore using the CData ODBC Driver for Lakebase from within PyCharm. Included are steps for adding the CData ODBC Driver as a data source, as well as basic PyCharm code to query the data source and display results.

To begin, this tutorial will assume that you have already installed the CData ODBC Driver for Lakebase as well as PyCharm.

Add Pyodbc to the Project

Follow the steps below to add the pyodbc module to your project.

  1. Click File -> Settings to open the project settings window.
  2. Click Project Interpreter from the Project: YourProjectName menu.
  3. To add pyodbc, click the + button and enter pyodbc.
  4. Click Install Package to install pyodbc.

Connect to Lakebase

You can now connect with an ODBC connection string or a DSN. See the Getting Started section in the CData driver documentation for a guide to creating a DSN on your OS.

To connect to Databricks Lakebase, start by setting the following properties:
  • DatabricksInstance: The Databricks instance or server hostname, provided in the format instance-abcdef12-3456-7890-abcd-abcdef123456.database.cloud.databricks.com.
  • Server: The host name or IP address of the server hosting the Lakebase database.
  • Port (optional): The port of the server hosting the Lakebase database, set to 5432 by default.
  • Database (optional): The database to connect to after authenticating to the Lakebase Server, set to the authenticating user's default database by default.

OAuth Client Authentication

To authenicate using OAuth client credentials, you need to configure an OAuth client in your service principal. In short, you need to do the following:

  1. Create and configure a new service principal
  2. Assign permissions to the service principal
  3. Create an OAuth secret for the service principal

For more information, refer to the Setting Up OAuthClient Authentication section in the Help documentation.

OAuth PKCE Authentication

To authenticate using the OAuth code type with PKCE (Proof Key for Code Exchange), set the following properties:

  • AuthScheme: OAuthPKCE.
  • User: The authenticating user's user ID.

For more information, refer to the Help documentation.

Below is the syntax for a DSN:

[CData Lakebase Source]
Driver = CData ODBC Driver for Lakebase
Description = My Description
DatabricksInstance = lakebase
Server = 127.0.0.1
Port = 5432
Database = my_database
InitiateOAuth = GETANDREFRESH

Execute SQL to Lakebase

Instantiate a Cursor and use the execute method of the Cursor class to execute any SQL statement.

import pyodbc

cnxn = pyodbc.connect('DRIVER={CData ODBC Driver for Lakebase};DatabricksInstance = lakebase;Server = 127.0.0.1;Port = 5432;Database = my_database;InitiateOAuth = GETANDREFRESH;')
cursor = cnxn.cursor()
cursor.execute("SELECT ShipName, ShipCity FROM Orders WHERE ShipCountry = 'USA'")
rows = cursor.fetchall()
for row in rows:
  print(row.ShipName, row.ShipCity)

After connecting to Lakebase in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to Lakebase data as if it were a standard database. If you have any questions, comments, or feedback regarding this tutorial, please contact us at [email protected].

Ready to get started?

Download a free trial of the Lakebase ODBC Driver to get started:

 Download Now

Learn more:

Lakebase Icon Lakebase ODBC Driver

The Lakebase ODBC Driver is a powerful tool that allows you to connect with live data from Lakebase, directly from any applications that support ODBC connectivity.

Access Lakebase data like you would a database - read, write, and update Lakebase 0, etc. through a standard ODBC Driver interface.