📄 141.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 -> DBM (Database Managers) Databases</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">DBM (Database Managers) Databases</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="140.html" title="Flat Databases"><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=141" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="141.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="142.html" title="Object Serialization and Persistent Storage"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A38%3A47+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147044209063089000142000</font><a href="read2.asp?bookname=0672319942&snode=141&now=5%2F31%2F2002+4%3A38%3A47+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
DBM (Database Managers) Databases</h3>
<p>Now, let's look at this other mechanism for storing data. The next modules store data in dbm-style format. This format specifies a simple disk-based storage facility that handles data in a way equivalent to dictionaries. The objects are manipulated by using unique key strings. Each of these modules is an interface to a specific library.</p>
<P>
<A NAme="idx1073744865"></a>
<a NAME="idx1073744866"></a>
<tt cLASS="monofont">dbm,</tt>
<a name="idx1073744867"></a>
<a name="idx1073744868"></a>
<tt clasS="monofont">gdbm,</tt> and <Tt clAss="monofont">dbhash</tt> are database modules that are part of the standard Python distribution.</P>
<p>Also included with the standard Python distribution is the <a nAME="idx1073744869"></A>
<a namE="idx1073744870"></A>
<TT clasS="monofont">anydbm</TT> module, which is a generic interface to all the dbm-like modules. It uses the modules that are installed.</P>
<p>The <a naME="idx1073744871"></A>
<A name="idx1073744872"></a>
<tt class="monofont">dbhash</tt> module provides a function that offers a dbm-style interface to access the BSD database library.</p>
<p>All these modules have some behavior in common. For example, to open the files, the following syntax is used by all of them.<a namE="idx1073744873"></a>
<a Name="idx1073744874"></A>
</p>
<pre>
dbhandle = open(<I>filename [, flag [,mode]]</i>)
</prE>
<P>Where, <I>filename</I> is the database filename; <i>flag</i> can have one of the following values: <a nAME="idx1073744875"></A>
<a namE="idx1073744876"></A>
<TT clasS="monofont">r</TT> (read-only access), <A name="idx1073744877"></a>
<a name="idx1073744878"></a>
<tt class="monofont">w</tt> (read/write access), <A naMe="idx1073744879"></a>
<a Name="idx1073744880"></a>
<Tt clASS="monofont">c</Tt> (create the database), <a naME="idx1073744881"></A>
<A name="idx1073744882"></A>
<TT Class="monofont">n</TT> (force the creation of a new database); and <A Name="idx1073744883"></a>
<a name="idx1073744884"></a>
<i>mode</i> specifies the file access mode (specific for UNIX systems).</p>
<p>The following operations are supported:</p>
<pre>
dbhandle[key] = value # Set the value of a given key entry
value = dbhandle[key] # Get the value of a given key entry
dbhandle.has_key(key) # Test whether a key exists
dbhandle.keys() # Returns a list of the current keys available
del dbhandle[key] # Delete a key
dbhandle.close() # Close the file
</prE>
<p>For all these dbm-like modules, the keys and the values to be stored must be of type <i>string.</I> Later, you will see a module called <a naMe="idx1073744885"></a>
<a nAme="idx1073744886"></a>
<TT CLass="monofont">shelve</tT> with a behavior similar to these dbm-like modules. However, it stores persistent objects.</P>
<P>Each <A name="idx1073744887"></A>
<A NAme="idx1073744888"></a>module provides its own exception, which is called <i>
<TT CLass="monofont">modulename.</tt>
</i>
<tt class="monofont">error.</tt>
</p>
<pre>
>>> import anydbm
>>> try:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -