Connecting Netezza using Python pyodbc – Working Example

  • Post author:
  • Post last modified:April 30, 2019
  • Post category:Netezza
  • Reading time:3 mins read

In this modern era, data plays very important role in every decision making. Most of applications that is written in various programming languages require access to relational databases to get required data for its process. Before you can run SQL statements to create, update, delete, or retrieve data, you must connect to a database. In this article, we will check process and working example on connecting Netezza using Python pyodbc driver.

Netezza also supports JDBC driver: How to Connect Netezza using JDBC Driver and working Examples

Connecting Netezza using Python pyodbc

Netezza ODBC drivers

Before attempting to connect to Netezza database from either windows or Linux system, you must first install Netezza ODBC or OLEDB drivers.

Configure ODBC on Linux

IBM has provided Netezza ODBC driver that you can install into any Linux box. Go to IBM support center and download required version of ODBC driver.

You can read on how to configure Netezza ODBC on drivers in my other post:

Install ODBC on Windows

You can download the Netezza odbc drivers for Netezza from IBM website and install it required system or server.

Follow below link for more information:

You will not be able to use pyodbc driver without installing Netezza OBDC drivers on required system. This step is one of the pre-requisites to use pyodbc.

Note that, you need to have support service agreement with IBM in order to download any Netezza or Puredata Systems related software’s.

Connecting Netezza using Python pyodbc

Once you have installed required drivers, you are now ready to use pyodbc to connect to Netezza.

Just verify the pyodbc using below statements. And make sure you have NZSQL data source in the list. You should have ‘NZSQL’: ‘NetezzaSQL’ in your pyodbc data source list.

You will not be able to connect to Netezza without NZSQL data source.

import pyodbc
print (pyodbc.dataSources())

{'Excel Files': 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'MS Access Database': 'Microsoft Access Driver (*.mdb, *.accdb)', 'dBASE Files': 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'NZSQL': 'NetezzaSQL'}

Connecting Netezza using Python pyodbc Example

If you have configured Netezza ODBC drivers properly and you have NZSQL data source in place, next step would be to write small Python script that uses pyodbc to connect to Netezza databases.

import pyodbc

# pyodbc connection string
conn = pyodbc.connect("DRIVER={NetezzaSQL};SERVER=192.168.0.10; PORT=5480;DATABASE=TESTDB; UID=admin;PWD=password;")

# Define Cursor
cus=conn.cursor()

# Execute SQL statement and store result in cursor
cus.execute("select * from TEST123;")

# Display the content of cursor
while True:
    row=cus.fetchone()
    if not row:
        break
    print(row)
Output:
(1, )
(2, )
(3, )

Related reading: