Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
235 lines (181 sloc) 8.92 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="">
<link rel="stylesheet" type="text/css" href="styles.css" />
<div id="titlebox">
pyodbc - A Python DB API module for ODBC
<div id="nav">
<a href="index.html">Home</a>
: <a href="docs.html">Documentation</a>
: <a href="">Download</a>
: <a href="">News</a>
: <a href="">Source Code (github)</a>
: <a href=";atid=824254">Bug&nbsp;Tracker</a>
: <a href="">SourceForge</a>
: <a href="">Python DB API</a>
: <a href="license.html">MIT License</a>
<div id="contents">
<h1>About pyodbc</h1>
<p>pyodbc is a Python module that allows you to access ODBC databases. It implements the
<a href="">Python Database API Specification v2.0</a>.</p>
<p>Some notable features include:</p>
<li>The library is free for commercial and personal use.</li>
<li>It conforms to the DB API standard.</li>
<li>No 3rd party libraries are required. Only native Python datatypes are used, such as decimal and datetime.</li>
<li>It requires Python 2.4 or higher, since the decimal type was added in 2.4. (We are open to suggestions regarding versions for
earlier Python builds.)</li>
<li>Additional features have been added to simplify database programming with Python.</li>
<h1>Additional Features</h1>
<p>The following features are beyond the requirements of the DB API. They are intended to provide a very
Python-like, convenient programming experience, but you should not use them if your code needs to be portable between
DB API modules. (Though we hope future DB API specifications will adopt some of these features.)</p>
<h2>Access Values By Name</h2>
<p>The DB API specifies that results must be tuple-like, so columns are normally accessed by indexing into the
sequence (e.g. <code>row[0]</code>) and pyodbc supports this. However, columns can also be accessed by name:</p>
cursor.execute("select <b>album_id</b>, <b>photo_id</b> from photos where user_id=1")
row = cursor.fetchone()
print <b>row.album_id</b>, <b>row.photo_id</b>
print row[0], row[1] # same as above, but less readable</pre>
<p>This makes the code easier to maintain when modifying SQL, more readable, and allows rows to be used where a
custom class might otherwise be used. All rows from a single <code>execute</code> share the same dictionary of
column names, so using Row objects to hold a large result set may also use less memory than creating a object for
each row.</p>
<p>The SQL "as" keyword allows the name of a column in the result set to be specified. This is useful if a column
name has spaces or if there is no name:</p>
cursor.execute("select count(*) <b>as photo_count</b> from photos where user_id=1")
row = cursor.fetchone()
print <b>row.photo_count</b></pre>
<h2>Rows Values Can Be Replaced</h2>
<p>Though SQL is very powerful, values sometimes need to be modified before they can be used. Rows allow their
values to be replaced, which makes them even more convenient ad-hoc data structures.</p>
# Replace the 'start_date' datetime in each row with one that has a time zone.
rows = cursor.fetchall()
for row in rows:
row.start_date = row.start_date.astimezone(tz)</pre>
<p>Note that columns cannot be added to rows; only values for existing columns can be modified.</p>
<h2>Cursors are Iterable</h2>
<p>The DB API makes this an optional feature. Each iteration returns a row object.</p>
cursor.execute("select album_id, photo_id from photos where user_id=1")
for row in cursor:
print row.album_id, row.photo_id
<h2>Cursor.execute Returns the "Right" Thing</h2>
<p>The DB API specification does not specify the return value of Cursor.execute, so pyodbc returns different types
based on the SQL statement executed.</p>
<p>A select statement returns the cursor itself, allowing the execute results to be iterated over and used to fetch.
This makes the code very compact:</p>
for row in cursor.execute("select album_id, photo_id from photos where user_id=1"):
print row.album_id, row.photo_id
row = cursor.execute("select count(*) from tmp").fetchone()
rows = cursor.execute("select * from tmp").fetchall()</pre>
<p>Update and delete statements return the number of rows affected:</p>
count = cursor.execute("update photos set processed=1 where user_id=1")
count = cursor.execute("delete from photos where user_id=1")
<p>All other SQL statements return <code>None</code>.</p>
<p>pyodbc Connection objects have an execute method that creates new Cursors automatically.</p>
for row in <strong>cnxn</strong>.execute("select user_id from tmp"):
print row.user_id</pre>
<p>Since each call creates a new cursor, do not use this when executing multiple statements in a row.</p>
<h2>Passing Parameters</h2>
<p>As specified in the DB API, Cursor.execute accepts an optional sequence of parameters:</p>
cursor.execute("select a from tbl where b=? and c=?", (x, y))</pre>
<p>However, pyodbc also accepts the parameters directly. Note that the parameters are not in a tuple:</p>
cursor.execute("select a from tbl where b=? and c=?", x, y)</pre>
<h2>Autocommit Mode</h2>
<p>Connections can be put into autocommit mode using the <code>autocommit</code> keyword of
the <a href="docs.html#connect">connect</a> function or
the <a href="docs.html#connection_autocommit">autocommit</a> attribute of the Connection
<h2 id="misc">Miscellaneous ODBC Functions</h2>
<p><a href="docs.html#connection_getinfo">Connection.getinfo</a> function is an interface to
<p>Most of the ODBC catalog functions are available as methods on Cursor objects. The results
are presented as SELECT results in rows that are fetched normally. Refer to Microsoft's ODBC
documentation for details of how to use each function.</p>
cnxn = pyodbc.connect(...)
cursor = cnxn.cursor()
for row in cursor.tables():
print row.table_name
<table border="0" cellspacing="4">
<td>ODBC Function</td>
<td><a href="docs.html#cursor_tables">Cursor.tables</a></td>
<td>Returns a list of table, catalog, or schema names, and table types.</td>
<td><a href="docs.html#cursor_columns">Cursor.columns</a></td>
<td>Returns a list of column names in specified tables.</td>
<td><a href="docs.html#cursor_statistics">Cursor.statistics</a></td>
<td>Returns a list of statistics about a single table and the indexes associated with the table.</td>
<td><a href="docs.html#cursor_rowid">Cursor.rowIdColumns</a></td>
<td>Returns a list of columns that uniquely identify a row.</td>
<td><a href="docs.html#cursor_rowver">Cursor.rowVerColumns</a></td>
<td>Returns a list of columns that are automatically updated any any value in the row is updated.</td>
<td><a href="docs.html#cursor_primarykeys">Cursor.primaryKeys</a></td>
<td>Returns a list of column names that make up the primary key for a table.</td>
<td><a href="docs.html#cursor_foreignkeys">Cursor.foreignKeys</a></td>
<td>Returns a list of column names that are foreign keys in the specified table (columns
in the specified table that refer to primary keys in other tables) or foreign keys in
other tables that refer to the primary key in the specified table.</td>
<td><a href="docs.html#cursor_procedures">Cursor.procedures</a></td>
<td>Returns information about the procedures in the data source.</td>
<td><a href="docs.html#cursor_gettypeinfo">Cursor.getTypeInfo</a></td>
<td>Returns a information about the specified data type or all data types supported by the driver.</td>
</div> <!-- contents -->
<hr />
<a href=""><img src=";type=4" width="125" height="37" border="0" alt=" Logo" /></a>
Something went wrong with that request. Please try again.