Using the CData ODBC Driver for USPS in PyCharm

Connect to USPS as an ODBC data source in PyCharm using the CData ODBC Driver for USPS.

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 USPS 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 USPS 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 USPS

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 authenticate with USPS, set the following connection properties.

  • PostageProvider: The postage provider to use to process requests. Available options are ENDICIA and STAMPS. If unspecified, this property will default to ENDICIA.
  • UseSandbox: This controls whether live or test requests are sent to the production or sandbox servers. If set to true, the Password, AccountNumber, and StampsUserId properties are ignored.
  • StampsUserId: This value is used for logging into authentication to the Stamps servers. This value is not applicable for Endicia and is optional if UseSandbox is true.
  • Password: This value is used for logging into Endicia and Stamps servers. If the postage provider is Endicia, this will be the pass phrase associated with your postage account. It is optional if UseSandbox is true.
  • AccountNumber: The shipper's account number. It is optional if UseSandbox is true.
  • PrintLabelLocation: This property is required to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

The Cache Database

Many of the useful task available from USPS require a lot of data. To ensure this data is easy to input and recall later, utilize a cache database to make requests. Set the cache connection properties in order to use the cache:

  • CacheLocation: The path to the cache location, for which a connection will be configured with the default cache provider. For example, C:\users\username\documents\uspscache

As an alternative to CacheLocation, set the combination of CacheConnection and CacheProvider to configure a cache connection using a provider separate from the default.

Below is the syntax for a DSN:

[CData USPS Source] Driver = CData ODBC Driver for USPS Description = My Description PostageProvider = ENDICIA RequestId = 12345 Password = 'abcdefghijklmnopqr' AccountNumber = '12A3B4C'

Execute SQL to USPS

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 USPS};PostageProvider = ENDICIA; RequestId = 12345; Password = 'abcdefghijklmnopqr'; AccountNumber = '12A3B4C'') cursor = cnxn.cursor() cursor.execute("SELECT FirstName, Phone FROM Senders WHERE SenderID = '25'") rows = cursor.fetchall() for row in rows: print(row.FirstName, row.Phone)

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