Connecting to ODBC databases from Python with pyodbc

The section below provides a Python code sample that shows you how to connect to a database using the functions of the pyodbc module and Devart ODBC driver that implements the ODBC standard in data mapping. This code also works, with slight modifications, for accessing data in cloud applications from Python, such as MailChimp, Salesforce, Dynamics CRM, BigCommerce, or QuickBooks.
Need to connect Python to an ODBC database such as Oracle, MySQL, or SQL Server? You can do it with an open-source Python module pyodbc, Devart ODBC driver, and a few lines of code. The pyodbc module implements the Python DB API 2.0 specification, which was designed to provide a consistent interface to various databases and help developers write apps that can work with different databases without significant code changes. Python applications use an ODBC driver manager and ODBC driver to connect to the database server.
To connect to a database and retrieve data using the Python progamming language, you only need a few functions:
  • connect() function to create a connection to the database;
  • cursor() function to create a cursor from the connection;
  • execute() function to execute a select statement;
  • fetchone() function to retrieve rows from the query.

Steps to connect to ODBC database in Python
with pyodbc module

Install the driver and configure an ODBC data source in the driver settings

01.Import the pyodbc module and create a connection to the database.

 
                                import pyodbc 
cnxn = pyodbc.connect('DRIVER={Devart ODBC Driver for SQLite};Direct=True;Database=mydatabase')



Start SQL Server Management Studio and create a linked server

02.Execute an INSERT statement to test the connection to the database.

 
                                cursor = cnxn.cursor()  
cursor.execute("INSERT INTO EMP (EMPNO, ENAME, JOB, MGR) VALUES (535, 'Scott', 'Manager', 545)")



Run a query against the data storage

03.Retrieve a result set from a query, iterate over it and print out all records.

   
                                    cursor.execute("SELECT * FROM EMP") 
row = cursor.fetchone()
while row:
print (row)
row = cursor.fetchone()

Key Features

Direct mode

Native connection to traditional databases and cloud-based services from Python using an ODBC driver. No need to use client software to fetch your sales figures table or a list of the best sales managers from popular data stores such as Oracle, MySQL, PostgreSQL, SQL Server, as well as cloud services such as Salesforce or Zoho CRM.

SQL data type mapping

Full support and mapping of data source-specific SQL data types to the standard ODBC data types. Access diverse data stores from Python through a unified interface with exceptional speed. You can create custom mappings between the data source fields and ODBC data types.

Secure connection

Devart ODBC drivers use SSL / SSH and HTTPS tunneling to protect user data as it flows between a remote server on the Internet and and the machine where your Python code is executed. Tunneling allows you to send communications securely between machines.

ANSI SQL-92 standard support for cloud services

Devart ODBC drivers address one of the major challenges of retrieving data from email marketing, CRM, accounting and other cloud applications using Python: their lack of adequate API support for SQL-92 standard SQL commands. Our drivers are designed to get the most out of cloud service APIs.

Why pick our ODBC drivers for your Python application?

Fully Unicode-compliant ODBC drivers

Fully Unicode-compliant ODBC drivers

Unicode is the universal standard for encoding characters in most languages. Our ODBC drivers provide a Unicode-compliant way to work with strings and characters in databases: they are capable of mapping Unicode function calls and string data types to ANSI encoding.

All major versions of data stores are supported

All major versions of data stores are supported

Our ODBC drivers can be used to interact and perform SQL CRUD operations on popular traditional databases and cloud services from Python, such as MySQL, Oracle, PostgreSQL, Salesforce, or MailChimp.

Regular feature update rollouts and timely bug fixes

Regular feature updates and timely bug fixes

We frequently release new versions of ODBC drivers with new capabilities or enhancements for already existing features. Changes and new features are added to the roadmap based on customer feedback. Bug fixes and minor improvements are released as nightly builds to resolve issues in a timely fashion.

Strict compliance with Microsoft ODBC 3.51 Specification

Strict compliance with Microsoft ODBC 3.51 Specification

Our ODBC drivers provide full support for the standard Open Database Connectivity API functions and data types. ODBC interface allows Python to connect to ODBC databases using the SQL language. Additionally, we have designed Advanced Connection String parameters that implement different types of behavior.

Exceptional SQL query performance and bulk updates

Exceptional SQL query performance and bulk updates

Our ODBC drivers allow you to perform bulk record update with Python in a single update statement while maintaining decent update speed. When you need to update a table with a million of records, you can use a single update operation which significantly reduces execution time compared to row-by-row operations.

Team of expert support engineers and community of developers

Team of expert support engineers and community of developers

Contact our friendly knowledgeable support team for assistance if have any questions about using our ODBC drivers with Python, or join our forum to discuss anything related to the database application development with other professional developers.

Connect Python app to your data source with our ODBC drivers

Our drivers provide Direct access to your databases and clouds from Python, which eliminates the use of database client libraries, simplifies the deployment process, and extends your application capabilities.

Choose Devart ODBC driver for your data source

You can try out any of these for free