本篇继续介绍游标的使用,使用游标来增删改记录
一.增加记录当你使用游标来插入记录的时候,游标将位于插入的记录的位置。游标提供了下面几个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();
}
分享到:
相关推荐
Oracle BerkeleyDB-JE je-6.0.11
BerkeleyDB-Core-C-GSG.pdf bdb c开发手册 英文版
Berkeley DB JE-7.0.6 jar包
Java-Edition-BerkeleyDB-3.1.0,国外的开源数据库,供大家参考
(二) Berkeley DB -- Access Method Configuration_iyangjian2005997_新浪博客.mht
BerkeleyDB和Sqlite是当前最流行的嵌入式开源数据库。
db-6.1.26.tar.gz berkeley db
Berkeley DB -- 入门知识和一个小例子_iyangjian2005997_新浪博客.mht
Life After BerkeleyDB- OpenLDAP's Memory-Mapped Database
关于berkeley db 4.6.2的介绍性文章,是了解berkeley db的技术文档。
berkeley db je-6.4.9.gz
Berkeley DB是一个开源的文件数据库,介于关系数据库与内存数据库之间,使用方式与内存数据库类似,它提供的是一系列直接访问数据库的函数,而不是像关系数据库那样需要网络通讯、SQL解析等步骤,本文件是早期版本
嵌入式数据库,功能强大,可用于如QQ本地的聊天记录数据库等,使用方便,效率高
官方版本,亲测可用
Berkeley DB 4.4.20 .tar.gz 来自https://www.oracle.com/database/technologies/related/berkeleydb-release-history.html
来自于oracle的BerkeleyDB帮助文件,里面有代码的。
1. Introduction to Berkeley DB ......... 1 About This Manual .......... 2 Berkeley DB Concepts ........... 2 Access Methods ......... 4 Selecting Access Methods ......... 4 Choosing between BTree and ...
文件类型的DB,存取速度快,操作方便,是以map方式key-value方式存取数据.兄弟大家共享!
BerkeleyDB:BerkeleyDB-对Berkeley DB 2.x或更高版本的Perl5访问
使用BerkeleyDB数据库,java语言开发需要的jar包资源,7.5.11版本