日期:2014-05-16  浏览次数:20484 次

BerkeleyDB-JE 使用BaseAPI(六)
本篇继续介绍游标的使用,使用游标来增删改记录
一.增加记录当你使用游标来插入记录的时候,游标将位于插入的记录的位置。游标提供了下面几个API来插入记录。
  • Cursor.put() 插入一条记录,如果记录的key值已存在于数据库中,当数据库不支持重复记录时,旧的记录会被替换;否则会插入一条重复记录。
  • Cursor.putNoDupData() 当要插入的记录键和值都存在的时候,则返回OperationStatus.KEYEXIST,并且新记录无法插入。
  • Cursor.putNoOverwrite() 当要插入的记录的键已存在的时候,则返回OperationStatus.KEYEXIST,并且新记录无法插入。

你可能会发现这三个API和之前讲的用Database插入记录的API非常相像。下面演示使用游标来插入记录:
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.delete()可以删除当前游标定位的记录。但是要注意的一点是当你删除了一条记录,Cursor.getCurrent()仍是指向刚删除的记录,如果这时你再次调用Cursor.delete()将会报错。
 
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.putCurrent()可以替换当前记录。但是要注意的是使用这个方法不能替换重复记录集中的数据,这是因为重复记录集中的数据是会根据值来排序,你插入的记录值有可能会违反这个排序规则。
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();
}