日期:2014-05-16 浏览次数:20484 次
Cursor cursor = null; try { ... // Database and environment open omitted for brevity ... DatabaseEntry key1 = new DatabaseEntry(key1str.getBytes("UTF-8")); DatabaseEntry data1 = new DatabaseEntry(data1str.getBytes("UTF-8")); DatabaseEntry key2 = new DatabaseEntry(key2str.getBytes("UTF-8")); DatabaseEntry data2 = new DatabaseEntry(data2str.getBytes("UTF-8")); DatabaseEntry data3 = new DatabaseEntry(data3str.getBytes("UTF-8")); // Open a cursor using a database handle cursor = myDatabase.openCursor(null, null); // Assuming an empty database. OperationStatus retVal = cursor.put(key1, data1); // SUCCESS retVal = cursor.put(key2, data2); // SUCCESS retVal = cursor.put(key2, data3); // SUCCESS if dups allowed, // KEYEXIST if not. } catch (Exception e) { // Exception handling goes here } finally { // Make sure to close the cursor cursor.close(); }
Cursor cursor = null; try { ... // Database and environment open omitted for brevity ... // Create DatabaseEntry objects // searchKey is some String. DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(); // Open a cursor using a database handle cursor = myDatabase.openCursor(null, null); // Position the cursor. Ignoring the return value for clarity OperationStatus retVal = cursor.getSearchKey(theKey, theData, LockMode.DEFAULT); // Count the number of records using the given key. If there is only // one, delete that record. if (cursor.count() == 1) { System.out.println("Deleting " + new String(theKey.getData(), "UTF-8") + "|" + new String(theData.getData(), "UTF-8")); cursor.delete(); } } catch (Exception e) { // Exception handling goes here } finally { // Make sure to close the cursor cursor.close(); }
Cursor cursor = null; try { ... // Database and environment open omitted for brevity ... // Create DatabaseEntry objects // searchKey is some String. DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8")); DatabaseEntry theData = new DatabaseEntry(); // Open a cursor using a database handle cursor = myDatabase.openCursor(null, null); // Position the cursor. Ignoring the return value for clarity OperationStatus retVal = cursor.getSearchKey(theKey, theData, LockMode.DEFAULT); // Replacement data String replaceStr = "My replacement string"; DatabaseEntry replacementData = new DatabaseEntry(replaceStr.getBytes("UTF-8")); cursor.putCurrent(replacementData); } catch (Exception e) { // Exception handling goes here } finally { // Make sure to close the cursor cursor.close(); }