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