BerkeleyDB-JE 使用BaseAPI(六)

本篇繼續介紹遊標的使用,使用遊標來增刪改記錄
一.增加記錄當你使用遊標來插入記錄的時候,遊標將位於插入的記錄的位置。遊標提供了下面幾個API來插入記錄。
[list]
[*]Cursor.put() 插入一條記錄,如果記錄的key值已存在於數據庫中,當數據庫不支持重複記錄時,舊的記錄會被替換;否則會插入一條重複記錄。
[*]Cursor.putNoDupData() 當要插入的記錄鍵和值都存在的時候,則返回OperationStatus.KEYEXIST,並且新記錄無法插入。
[*]Cursor.putNoOverwrite() 當要插入的記錄的鍵已存在的時候,則返回OperationStatus.KEYEXIST,並且新記錄無法插入。
[/list]
你可能會發現這三個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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章