1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Using Secondary Cursors</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="indexes.html" title="Chapter��10.��Secondary Databases" /> 11 <link rel="prev" href="secondaryDelete.html" title="Deleting Secondary Database Records" /> 12 <link rel="next" href="joins.html" title="Database Joins" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center"> 19 <span>Using Secondary Cursors</span> 20 21 </th> 22 </tr> 23 <tr> 24 <td width="20%" align="left"><a accesskey="p" href="secondaryDelete.html">Prev</a>��</td> 25 <th width="60%" align="center">Chapter��10.��Secondary Databases</th> 26 <td width="20%" align="right">��<a accesskey="n" href="joins.html">Next</a></td> 27 </tr> 28 </table> 29 <hr /> 30 </div> 31 <div class="sect1" lang="en" xml:lang="en"> 32 <div class="titlepage"> 33 <div> 34 <div> 35 <h2 class="title" style="clear: both"><a id="secondaryCursor"></a> 36 <span>Using Secondary Cursors</span> 37 38 </h2> 39 </div> 40 </div> 41 </div> 42 <p> 43 Just like cursors on a primary database, you can use 44 <span>secondary cursors</span> 45 46 to iterate over the records in a secondary database. Like 47 48 <span>normal cursors,</span> 49 50 51 you can also use 52 <span>secondary cursors</span> 53 54 to search for specific records in a database, to seek to the first 55 or last record in the database, to get the next duplicate record, 56 57 and so forth. For a complete description on cursors and their capabilities, see 58 <a class="xref" href="Cursors.html" title="Chapter��9.��Using Cursors">Using Cursors</a>. 59 </p> 60 <p> 61 However, when you use 62 <span>secondary cursors:</span> 63 64 </p> 65 <div class="itemizedlist"> 66 <ul type="disc"> 67 <li> 68 <p> 69 Any data returned is the data contained on the primary database 70 record referenced by the secondary record. 71 </p> 72 </li> 73 <li> 74 <p> 75 <code class="methodname">SecondaryCursor.getSearchBoth()</code> and 76 related methods do not search based on a key/data pair. Instead, you 77 search based on a secondary key and a primary key. The data returned 78 is the primary data that most closely matches the two keys provided 79 for the search. 80 </p> 81 </li> 82 </ul> 83 </div> 84 <p> 85 For example, suppose you are using the databases, classes, and key 86 <span>creators</span> 87 88 described in <a class="xref" href="keyCreator.html" title="Implementing Key Creators">Implementing Key 89 <span>Creators</span> 90 91 </a>. 92 Then the following searches for a person's 93 name in the secondary database, and deletes all secondary and primary 94 records that use that name. 95 </p> 96 <a id="java_index8"></a> 97 <pre class="programlisting">package db.GettingStarted; 98 99import com.sleepycat.db.DatabaseEntry; 100import com.sleepycat.db.LockMode; 101import com.sleepycat.db.OperationStatus; 102import com.sleepycat.db.SecondaryDatabase; 103import com.sleepycat.db.SecondaryCursor; 104 105... 106try { 107 SecondaryDatabase mySecondaryDatabase = null; 108 // Database opens omitted for brevity 109 ... 110 111 String secondaryName = "John Doe"; 112 DatabaseEntry secondaryKey = 113 new DatabaseEntry(secondaryName.getBytes("UTF-8")); 114 115 DatabaseEntry foundData = new DatabaseEntry(); 116 117 SecondaryCursor mySecCursor = 118 mySecondaryDatabase.openSecondaryCursor(null, null); 119 120 OperationStatus retVal = mySecCursor.getSearchKey(secondaryKey, 121 foundData, 122 LockMode.DEFAULT); 123 while (retVal == OperationStatus.SUCCESS) { 124 mySecCursor.delete(); 125 retVal = mySecCursor.getNextDup(secondaryKey, 126 foundData, 127 LockMode.DEFAULT); 128 } 129} catch (Exception e) { 130 // Exception handling goes here 131}</pre> 132 </div> 133 <div class="navfooter"> 134 <hr /> 135 <table width="100%" summary="Navigation footer"> 136 <tr> 137 <td width="40%" align="left"><a accesskey="p" href="secondaryDelete.html">Prev</a>��</td> 138 <td width="20%" align="center"> 139 <a accesskey="u" href="indexes.html">Up</a> 140 </td> 141 <td width="40%" align="right">��<a accesskey="n" href="joins.html">Next</a></td> 142 </tr> 143 <tr> 144 <td width="40%" align="left" valign="top">Deleting Secondary Database Records��</td> 145 <td width="20%" align="center"> 146 <a accesskey="h" href="index.html">Home</a> 147 </td> 148 <td width="40%" align="right" valign="top">��Database Joins</td> 149 </tr> 150 </table> 151 </div> 152 </body> 153</html> 154