热门关键字:  ubuntu  分区  Fedora  linux系统进程  函数

Berkeley DB——Cursor

来源: 作者: 时间:2007-12-03 Tag: 点击:
Introduction
Berkeley DB的游标(Dbc)和关系数据库的游标是类似的——一种可以迭代数据库中的记录的装置。对于重复记录,使用游标来访问他们会更加方便(使用Dbbulk get来得到多条记录是性能最高的做法)。另外,通过游标可以一条条地操作(修改和删除)记录。
在使用游标之前,必须先使用Db::cursor方法打开游标:
int Db::cursor(DbTxn *txnid, Dbc **cursorp, u_int32_t flags);
游标打开后可以通过游标searchwrite记录。使用完毕后,必须关闭之:
int Dbc::close(void);
例子:
#include <db_cxx.h>
...
Dbc *cursorp;
Db my_database(NULL, 0);
// Database open omitted for clarity
// Get a cursor
my_database.cursor(NULL, &cursorp, 0);
// Database and cursor open omitted for clarity
// Do something…
if (cursorp != NULL)
    cursorp->close();
Getting Records from DB Using Cursor
打开数据库,并打开此数据库的游标后,就可以使用该游标来search DB来遍历符合条件的记录了。
Dbc::get方法的flag参数可以取多个值,常用的有:
Flag
Description
DB_CURRENT
返回游标当前所指的key/data
DB_FIRST
游标指向第一条记录,并返回该key/data
DB_GET_BOTH
只有key/data都匹配才返回该key/data
DB_GET_RECNO
返回行号。数据库必须是B树的,且其flagDB_RECNUM
DB_LAST
DB_FIRST对应
DB_NEXT
游标指向下条记录,并返回下个key/data
DB_PREV
DB_NEXT对应
DB_MULTIPLE
返回该key的所有记录。数据库是允许重复记录的。
例子:
#include <db_cxx.h>
...
Db my_database(NULL, 0);
Dbc *cursorp;
try {
    // Database open omitted for clarity
    // Get a cursor
    my_database.cursor(NULL, &cursorp, 0);
    Dbt key, data;
    int ret;
    // Iterate over the database, retrieving each record in turn.
    while ((ret = cursorp->get(&key, &data, DB_NEXT)) == 0) {
        // Do interesting things with the Dbts here.
    }
    if (ret != DB_NOTFOUND) {
        // ret should be DB_NOTFOUND upon exiting the loop.
        // Dbc::get() will by default throw an exception if any
        // significant errors occur, so by default this if block
        // can never be reached.
    }
} catch(DbException &e) {
        my_database.err(e.get_errno(), "Error!");
} catch(std::exception &e) {
        my_database.errx("Error! %s", e.what());
}
// Cursors must be closed
if (cursorp != NULL)
    cursorp->close();
my_database.close(0);
Operating Records Using Cursor
Dbc对象提供了putdelget方法,通过这些方法我们可以写入、删除和获取记录。
例子:
#include "stdafx.h"
#include <iostream>
#include <db_cxx.h>
#include <string.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
       Db db(NULL,0);
      
       u_int32_t oFlags = DB_CREATE; // Open flags;
       try
       {
              db.open(NULL,                // Transaction pointer
                     "my_db.db",          // Database file name
            NULL,                // Optional logical database name
            DB_BTREE,            // Database access method
            oFlags,              // Open flags
            0);                  // File mode (using defaults)
              db.truncate(NULL,0,0);
              float money = 122.45;
              char *description = "Grocery bill.";
              Dbt key(&money, sizeof(float));
              Dbt data(description, strlen(description)+1);
              int ret = db.put(NULL, &key, &data, DB_NOOVERWRITE);
              cout<<"put data--"<<description<<endl;
              ret = db.get(NULL, &key, &data, DB_GET_BOTH);
              cout<<"get key--"<<*((float*)key.get_data())<<endl;
              cout<<"get data--"<<(char *)data.get_data()<<endl;
              money = 111;
              description = "James--------------------";
             data.set_data(description);
              data.set_size(strlen(description)+1);
              db.put(NULL,&key,&data,DB_NOOVERWRITE);
              ret = db.get(NULL, &key, &data, DB_GET_BOTH);
              cout<<"get key--"<<*((float*)key.get_data())<<endl;
              cout<<"get data--"<<(char *)data.get_data()<<endl;
              money = 191;
              description = "Mike";
              data.set_data(description);
              data.set_size(strlen(description)+1);
              db.put(NULL,&key,&data,DB_NOOVERWRITE);
              ret = db.get(NULL, &key, &data, DB_GET_BOTH);
              cout<<"get key--"<<*((float*)key.get_data())<<endl;
              cout<<"get data--"<<(char *)data.get_data()<<endl;
              Dbc* cursor;
              db.cursor(NULL,&cursor,0);
              cout<<"open cursor"<<endl;
             
              while((ret = cursor->get(&key,&data,DB_PREV)) != DB_NOTFOUND)
              {
                     cout<<"get key--"<<*((float*)key.get_data())<<endl;
                     cout<<"get data--"<<(char *)data.get_data()<<endl;
              }
              if (cursor != NULL)
              {
                     cursor->close();
              }
              money = 191;
              description = "Mike";
              data.set_data(description);
              data.set_size(strlen(description)+1);
              db.cursor(NULL,&cursor,0);
              cout<<"delete 191..."<<endl;
              while((ret = cursor->get(&key,&data,DB_SET)) == 0 )
              {
                     cursor->del(0);
              }
              if (cursor != NULL)
              {
                     cursor->close();
              }
              cout<<"after delete 191..."<<endl;
              db.cursor(NULL,&cursor,0);
              while((ret = cursor->get(&key,&data,DB_PREV)) != DB_NOTFOUND)
              {
                     cout<<"get key--"<<*((float*)key.get_data())<<endl;
                     cout<<"get data--"<<(char *)data.get_data()<<endl;
              }
              if (cursor != NULL)
              {
                     cursor->close();
              }
       }
       catch(DbException &e)
       {
              cerr<<"DBException:"<<e.what();
       }
       catch(std::exception &e)
       {
              cerr<<"DBException:"<<e.what();
       }
       system("pause");
       return 0;
}



相关文章:
SQLite3 C/C++ 开发接口简介(API函数) 二
SQLite3 使用教学
Scaling out MySQL - Hardware today and tomorro
SQLite适用的范围
sybase cursor declare
SQL语句大全精要
用户配额
decode函数
Oracle 数据库创建表空间、创建用户指定表空间
MySQL教会我使用GnuPG验证软件包
MySQL中MyISAM引擎与InnoDB引擎性能简单测试
initdb
mysqladmin在线帮助文档
CentOS 4.7 安装Oracle 9.2.0.4的一些问题
文本如何导入oracle(sqlldr 的用法)
ORA-00600: 内部错误代码,自变量: [16201], [],
分布式Oracle的database link
install a MySQL source distribution
Mysql备份脚本(未验证)
oracle 9.2.0.1 update 9.2.0.5
每小时Dump所有mysql数据库到NAS存储设备上
Oracle SCN机制解析
Account LOCKED(TIMED)
MySQL数据库5.0的my.cnf配置选项
oracle存储过程中调用其他用户的表
修改sqlplus提示符
MYSQL建表实例
一些对Mysql DBA有用的脚本
Oracle DBA 强悍挑战OS 64位 Solaris 10-真正休
[Bugzilla]由Mysql迁移到Oracle的方法