📄 143.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> The ODBC Module</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="137.html" class="navtitle">8. Working with Databases</a> > <span class="nonavtitle">The ODBC Module</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="142.html" title="Object Serialization and Persistent Storage"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=143" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="143.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="144.html" title="ADO (ActiveX Data Objects)"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A39%3A05+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147045098084001118189099</font><a href="read1.asp?bookname=0672319942&snode=143&now=5%2F31%2F2002+4%3A39%3A05+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
The ODBC Module</h3>
<p>
<i>ODBC (Open Database Connectivity)</I> is a standard interface created by Microsoft; hence, it is fully supported by the Windows platform. It provides access to almost every database. Currently, the ODBC implements the ANSI standard SQL3.</P>
<P>To configure the ODBC settings for a database in your Windows system, you must use the ODBC Data Source Administrator, which is located at the Windows Control Panel.</P>
<p>The two major advantages of choosing to code an application to the ODBC API are as follows:</p>
<ul>
<LI>
<P>
<B>Portable Data Access Code棤</b>
The ODBC API is available on all major databases.
</p>
</li>
<LI>
<P><B>
Dynamic Data Binding棤</b>
This allows the user or the system administrator to easily configure an application to use any ODBC compliant data source. This is perhaps the single biggest advantage of coding an application to the ODBC API. Dynamic binding allows the end user to pick a data source梩hat is, an SQL Server梐nd use it for all data applications without having to worry about recompiling the application. The ODBC module implements the Python DB API, so you can get this level of abstraction at the DB API level. Also, you don't explicitly recompile Python code.<a name="idx1073745003"></a>
<a name="idx1073745004"></a>
<a name="idx1073745005"></a>
</p>
</Li>
</uL>
<p>
<a nAme="idx1073745006"></a>EShop kindly donated the <tT claSS="monofont">ODBC</TT> module to the public domain. This module is included in the PythonWin distribution. For more details, check out the site at <a tarGET="_blank" Href="http://www.python.org/windows/win32/odbc.html">http://www.python.org/windows/win32/odbc.html</a>.</P>
<P>The next example shows how you can <A Name="idx1073745007"></a>
<A NAMe="idx1073745008"></a>open a ODBC connection using Python.</p>
<pre>
import dbi, odbc
try:
connection = odbc.odbc('DSN=mydatabase;UID=mylogin;
PASSWORD=mypassword')
cursor = connection.cursor()
cursor.execute('select name, email from USERS')
while 1:
record = cursor.fetchone()
if not record: break
print record
connection.close()
except NameError,e:
print 'NameError: ', e
</pre>
<p>Three ways (at least) to access ODBC from Python on the Windows platform are as follows:</p>
<ul>
<li>
<p>
<b>DB API棤</b>
Python Database API
</p>
</lI>
<li>
<P>
<b>calldll棤</b>
Sam Rushing's calldll module
</p>
</Li>
<li>
<p>
<B>DAO棤</b>
Microsoft Data Access Objects
</p>
</lI>
</UL>
<H4>
ODBC Example for Windows Platforms</h4>
<p>The first thing you need is to create a DSN for your database in the ODBC Data Source Administrator.</p>
<p>The PythonWin distribution comes with an <TT CLass="monofont">odbc</tT> module, which by the way is very stable. However, it is no longer going to be improved. This <TT Class="monofont">odbc</TT> module works along with the <A Name="idx1073745009"></a>
<a name="idx1073745010"></a>
<tt class="monofont">dbi</tt> module. Both files conform to the Version 1.0 of the Python Database API, providing a minimum implementation.</p>
<P>The whole ODBC functionality is made up of two extension files:</p>
<uL>
<li>
<p>
<Tt claSs="monofont">odbc.pyd
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -