Black Friday is here! Get up to 30% off on Devart products!
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

Connecting to ODBC databases with pyodbc in Python

Python is a high-level object-oriented programming language with many ready-made libraries designed to solve specific problems: creating multimedia applications, working with artificial intelligence and machine learning, developing back-end applications, database access, etc.

The availability of ready-made libraries makes the process of interacting with databases such as Oracle, MySQL, SQL Server, and others and cloud data sources such as Salesforce, Dynamics 365, Zoho CRM, etc., from Python easier and faster.

Need to connect Python to an ODBC database? You can do it with an open-source Python module pyodbc that implements the Python DB API 2.0 specification. This specification is designed to provide a consistent interface to various databases and help developers write applications that can work with different databases without significant code changes. A Python ODBC module allows to organize a code logically that makes it easier to understand and use. The pyodbc module allows connecting to data sources from Python on Windows, macOS, and Linux, for both 32-bit and 64-bit platforms. You can easily install the pyodbc module on your machine using the pip install pyodbc command in Python interactive mode.
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.

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, and 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 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.

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()   

ODBC Data Connectors

Fast and unified access to databases

We offer you optimal data access by means of ODBC and native database client libraries. However, you can avoid using client libraries, since our ODBC drivers are able to connect via the native protocol directly without using client software at all.

Didn't find the necessary driver? Contact us!
Direct access to data

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.52 Specification

Strict compliance with Microsoft ODBC 3.52 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 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.

Frequently Asked Questions

What is pyodbc connect?

pyodbc is a Python open-source module that simplifies access to ODBC databases such as Oracle, MySQL, PostgreSQL, SQL Server, etc. A module implements the DB API 2.0 specification created to provide a consistent interface to various databases and help developers write applications that can work with different databases without significant changes in the code.

What is ODBC in Python?

ODBC (Open Database Connectivity) — is an Application Programming Interface (API) designed to access data storage. Many databases are supplied with an ODBC drivers, so you can use any of them with the pyodbc interface to access databases such as SQL Server, Oracle, PostgreSQL as well or cloud applications such as Streak, Zoho CRM, BigCommerce, etc. from your Python application.

How to Connect Python to SQL Server Using pyodbc?
  1. Connect to the SQL Server database by importing the pyodbc module and creating a connection to the database.
  2. Execute an insert statement to test the connection to the database. The script inserts a new record to the EMP table.
  3. Retrieve data from the SQL Server: use the cursor.execute() function to retrieve rows from the select query on a datase and the cursor.fetchone() function to iterate over the result set returned by cursor.execute(). To print all records from the table to the console, use the print() function.

Choose Devart ODBC driver for your data source

You can try out any of these for free