Ready to get started?

Learn more about the CData ODBC Driver for Magento or download a free trial:

Download Now

Using the CData ODBC Driver for Magento in PyCharm

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

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

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.

Magento uses the OAuth 1 authentication standard. To connect to the Magento REST API, you will need to obtain values for the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties by registering an app with your Magento system. See the "Getting Started" section in the help documentation for a guide to obtaining the OAuth values and connecting.

You will also need to provide the URL to your Magento system. The URL depends on whether you are using the Magento REST API as a customer or administrator.

  • Customer: To use Magento as a customer, make sure you have created a customer account in the Magento homepage. To do so, click Account -> Register. You can then set the URL connection property to the endpoint of your Magento system.

  • Administrator: To access Magento as an administrator, set CustomAdminPath instead. This value can be obtained in the Advanced settings in the Admin menu, which can be accessed by selecting System -> Configuration -> Advanced -> Admin -> Admin Base URL.

    If the Use Custom Admin Path setting on this page is set to YES, the value is inside the Custom Admin Path text box; otherwise, set the CustomAdminPath connection property to the default value, which is "admin".

Below is the syntax for a DSN:

[CData Magento Source] Driver = CData ODBC Driver for Magento Description = My Description OAuthClientId = MyConsumerKey OAuthClientSecret = MyConsumerSecret CallbackURL = http://127.0.0.1:33333 Url = https://mymagentohost.com

Execute SQL to Magento

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 Magento};OAuthClientId = MyConsumerKey;OAuthClientSecret = MyConsumerSecret;CallbackURL = http://127.0.0.1:33333;Url = https://mymagentohost.com;') cursor = cnxn.cursor() cursor.execute("SELECT Name, Price FROM Products WHERE Style = 'High Tech'") rows = cursor.fetchall() for row in rows: print(row.Name, row.Price)

After connecting to Magento in PyCharm using the CData ODBC Driver, you will be able to build Python apps with access to Magento 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.