Skip to content
A pure Python Cross Platform ODBC interface module
Python C
Branch: master
Clone or download
braian87b Merge pull request #52 from pc84/master
Fixing compatibility with linux freetds, fixing Exception TypeError '…
Latest commit 8e36abe Apr 3, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
headerfiles Version 1.0.2 Feb 28, 2013
Logo.gif
README.md
_config.yml Set theme jekyll-theme-minimal Jan 28, 2017
accesstests.py
benchmark.bat Version 1.0.2 Feb 28, 2013
empty.mdb Version 1.0.2 Feb 28, 2013
exceltests.py add back test.xls from pyodbc & fix to get excel test work again Mar 31, 2017
pypyodbc.py Merge pull request #52 from pc84/master Apr 3, 2019
setup.py
speed.py Update speed.py Jan 26, 2017
sqlservertests.py Update sqlservertests.py Mar 26, 2013
test.py Ansi/Unicode functions enhancement Mar 6, 2013
test.xls add back test.xls from pyodbc & fix to get excel test work again Mar 31, 2017
testutils.py Version 1.0.2 Feb 28, 2013

README.md

pypyodbc

A pure Python Cross Platform ODBC interface module

This is the GitHub homepage of pypyodbc

Also check out the Wiki and Version History

Features

  • One pure Python script, runs on CPython / IronPython / PyPy , Python 3.3 / 2.4 / 2.5 / 2.6 / 2.7 , Win / Linux , 32 / 64 bit, now also seems to work on Mac with little effor: How to use pypyodbc on MacOS OSX
  • Almost totally same usage as pyodbc (can be seen as a re-implementation of pyodbc in pure Python via ctypes)
  • Simple - the whole module is implemented in a single python script with less than 3000 lines
  • Built-in Access MDB file creation and compression functions on Windows

Simply try pypyodbc:

# Microsoft Access DB
import pypyodbc 

connection = pypyodbc.win_create_mdb('D:\\database.mdb')

SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));'
connection.cursor().execute(SQL)
connection.close()
#SQL Server 2000/2005/2008 (and probably 2012 and 2014)
import pypyodbc as pyodbc # you could alias it to existing pyodbc code (not every code is compatible)
db_host = 'serverhost'
db_name = 'database'
db_user = 'username'
db_password = 'password'
connection_string = 'Driver={SQL Server};Server=' + db_host + ';Database=' + db_name + ';UID=' + db_user + ';PWD=' + db_password + ';'
db = pyodbc.connect(connection_string)
SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));'
db.cursor().execute(SQL)

# Doing a simple SELECT query
connStr = (
    r'Driver={SQL Server};'
    r'Server=sqlserver;'
    #r'Server=127.0.0.1,52865;' +
    #r'Server=(local)\SQLEXPRESS;'
    r'Database=adventureworks;'
    #r'Trusted_Connection=Yes;'
    r'UID=sa;'
    r'PWD=sapassword;'
    )
db = pypyodbc.connect(connStr)
cursor = db.cursor()

# Sample with just a raw query:
cursor.execute("select client_name, client_lastname, [phone number] from Clients where client_id like '01-01-00%'")

# Using parameters (IMPORTANT: YOU SHOULD USE TUPLE TO PASS PARAMETERS)
# Python note: a tuple with just one element must have a trailing comma, otherwise is just a enclosed variable
cursor.execute("select client_name, client_lastname, [phone number] "
"from Clients where client_id like ?", ('01-01-00%', ))

# Sample, passing more than one parameter
cursor.execute("select client_name, client_lastname, [phone number] "
"from Clients where client_id like ? and client_age < ?", ('01-01-00%', 28))

# Method 1, simple reading using cursor
while True:
    row = cursor.fetchone()
    if not row:
        break
    print("Client Full Name (phone number): ", row['client_name'] + ' ' +  row['client_lastname'] + '(' + row['phone number'] + ')')

# Method 2, we obtain dict's all records are loaded at the same time in memory (easy and verbose, but just use it with a few records or your app will consume a lot of memory), was tested in a modern computer with about 1000 - 3000 records just fine...
import pprint; pp = pprint.PrettyPrinter(indent=4)
columns = [column[0] for column in cursor.description]
for row in cursor.fetchall():
    pp.pprint(dict(zip(columns, row)))

# Method 3, we obtain a list of dict's (represents the entire query)
query_results = [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
pp.pprint(query_results)

# When cursor was used must be closed, if you will not use again the db connection must be closed too.
cursor.close()
db.close()

How to use it without install (the latest version from here)

Just copy the latest pypyodbc.py downloaded from this repository on your project folder and import the module.

Install

If you have pip available (keep in mind that the version on pypi may be old):

pip install pypyodbc

Or get the latest pypyodbc.py script from GitHub (Main Development site) https://github.com/jiangwen365/pypyodbc

python setup.py install
You can’t perform that action at this time.