Fetching contributors…
Cannot retrieve contributors at this time
123 lines (90 sloc) 4.46 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="tutorial.html">Tutorial</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>
<div id="contents">
<p>This document is high-level introduction to using pyodbc and does not cover all its
details. pyodbc implements the
<a href="">Python Database API Specification
v2.0</a>, so you should read this specification for more information.</p>
<p>If you haven't installed pyodbc,
<a href="">download</a> and
install it.</p>
<p>First, you must import pyodbc. If you get errors here, make sure you have pyodbc installed.</p>
import pyodbc</pre>
<p>Next, create a connection by passing an ODBC connection string to the connect method. This
step causes ODBC to load the database driver (the SQL Server driver in this example) and
connect to the database.</p>
cnxn = pyodbc.connect('DSN=northwind')</pre>
<p>The ODBC connection string format is specified by ODBC in the
documentation. Unfortunately, this is for C programmers, but the comments section discussion
of the connection string format is useful.</p>
<p>ODBC itself recognizes the following keywords in a connection string:</p>
<dd>The ODBC driver to use. Make sure the driver you want to use is installed.</dd>
<dd>The name of a DSN configured in the control panel Data Sources applet. This allows
database information to be specified in an application-independent manner and location.</dd>
<dd>The user name when a login is required.</dd>
<dd>The password when a login is required. DSNs cannot contain passwords, so you may need
this even when using the DSN keyword.</dt>
<dd>The name of a .dsn file, used when the DSN information is stored in a file.</dd>
<p>Each database driver may support additional keywords. For example, the SQL Server driver
allows you to specify the machine SQL Server is running on using the SERVER keyword and the
database to connect to using the DATABASE keyword. These two allow you to connect to the
database without registering a DSN in the control panel. (The ODBC section of the SQL Native
Client <a href="">Using
Connection String Keywords with SQL Native Client</a> documentation may be useful when using SQL Server.)</p>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=user;PWD=password')</pre>
<h1>Create an Example Table</h1>
<p>Next, we'll create a table and populate it with some example values. First, make a cursor
and execute the necessary SQL. (The SQL may need to be modified for your database,
particularly the type names like 'int'. I'm testing this using SQL Server.) Finally, commit
the changes.</p>
cursor = cnxn.cursor()
cursor.execute("create table tmp(a int, b varchar(30))")
<p>First, notice that the commit is applied to the connection, not the cursor. Changes from
all cursors attached to the same connection will be commited. Also note that the commit
is <i>required</i>. If you do not commit, the changes will be rolled back when the connection
is closed.</p>
<h1>Insert Some Values</h1>
<h1>Selecting Values</h1>
<p>Once you have a connection, obtain a cursor from it and execute a select statement via the
cursor's <code>execute</code> method:</p>
cursor = cnxn.cursor()
cursor.execute('select a, b from tmp')