BTree读写
- Db db(NULL,DB_CXX_NO_EXCEPTIONS);//环境指针
- //事务指针,数据库类别,访问标志(创建,只读,自动提交事务,线程安全),mode在windows中忽略
- if(db.open(NULL,"test.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"同名数据库存在"<<endl;
- }
- //基本写
- int nKey=1;
- char szBuf[100]="愚蠢的人类啊";
- Dbt key,value;
- key.set_data(&nKey);
- key.set_size(4);
- value.set_data(szBuf);
- value.set_size(sizeof(szBuf));
- if(db.put(NULL,&key,&value,0)!=0)
- {
- cout<<"写入失败"<<endl;
- }
- //基本读
- char szBufRead[100]={0};
- Dbt readKey,readValue;
- readKey.set_data(&nKey);
- readKey.set_size(4);
- readValue.set_data(szBufRead);
- readValue.set_ulen(100);
- readValue.set_flags(DB_DBT_USERMEM);
- if(db.get(NULL,&readKey,&readValue,0)!=0)
- {
- cout<<"读取失败"<<endl;
- }
- cout<<szBufRead<<endl;
Queue读写
- Db db(NULL,DB_CXX_NO_EXCEPTIONS);//环境指针
- db.set_re_len(100);
- db.set_re_pad(0);
- //事务指针,数据库类别,访问标志(创建,只读,自动提交事务,线程安全),mode在windows中忽略
- if(db.open(NULL,"DB\\test.db",NULL,DB_QUEUE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- std::cout<<"同名数据库存在"<<std::endl;
- }
- //基本写
- char szBuf[100]="愚蠢的人类啊";
- int nkey=0;
- Dbt key(&nkey,sizeof(nkey));
- Dbt value;
- value.set_data(szBuf);
- value.set_size(sizeof(szBuf));
- if(db.put(NULL,&key,&value,DB_APPEND)!=0)
- {
- cout<<"写入失败"<<endl;
- }
- cout<<"返回的记录号为:"<<*((int*)key.get_data())<<endl;
游标基本读写删
- Db db(NULL,DB_CXX_NO_EXCEPTIONS);//环境指针
- //事务指针,数据库类别,访问标志(创建,只读,自动提交事务,线程安全),mode在windows中忽略
- if(db.open(NULL,"test.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"同名数据库存在"<<endl;
- }
- //基本写
- int nKey=1;
- char szBuf[100]="愚蠢的人类啊";
- Dbt key,value;
- key.set_data(&nKey);
- key.set_size(4);
- value.set_data(szBuf);
- value.set_size(sizeof(szBuf));
- for(int i=0;i<100;++i)
- {
- nKey=i;
- if(db.put(NULL,&key,&value,0)!=0)
- {
- cout<<"写入失败"<<endl;
- }
- }
- //游标读
- Dbc *p=0;
- db.cursor(NULL,&p,0);
- Dbt key1,value1;
- while(p->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<"键:"<<(*((int *)key1.get_data()))<<"值:"<<(char*)(value1.get_data())<<endl;
- }
- //备注:游标没有函数返回当前有多少个键
- //查找
- nKey=80;
- key1.set_data(&nKey);
- key1.set_size(sizeof(int));
- p->get(&key1,&value1,DB_SET);
- p->del(0);
- //游标读
- Dbc *p1=0;
- db.cursor(NULL,&p1,0);
- while(p1->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<"键:"<<(*((int *)key1.get_data()))<<"值:"<<(char*)(value1.get_data())<<endl;
- }
数据库支持重复记录
- Db db(NULL,DB_CXX_NO_EXCEPTIONS);//环境指针
- db.set_flags(DB_DUP);
- //事务指针,数据库类别,访问标志(创建,只读,自动提交事务,线程安全),mode在windows中忽略
- if(db.open(NULL,"test.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"同名数据库存在"<<endl;
- }
- //基本写
- int nKey=1;
- char szBuf[100]="愚蠢的人类啊";
- Dbt key,value;
- key.set_data(&nKey);
- key.set_size(4);
- value.set_data(szBuf);
- value.set_size(sizeof(szBuf));
- for(int i=0;i<5;++i)
- {
- if(db.put(NULL,&key,&value,0)!=0)
- {
- cout<<"写入失败"<<endl;
- }
- }
- //游标读
- Dbc *p=0;
- db.cursor(NULL,&p,0);
- Dbt key1,value1;
- while(p->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<"键:"<<(*((int *)key1.get_data()))<<"值:"<<(char*)(value1.get_data())<<endl;
- }
基本环境操作
- DbEnv evn(0);
- if(evn.open("DB",DB_CREATE|DB_INIT_MPOOL,0)!=0)
- {
- cout<<"打开环境错误"<<endl;
- return 0;
- }
- Db db(&evn,DB_CXX_NO_EXCEPTIONS);//环境指针
- //事务指针,数据库类别,访问标志(创建,只读,自动提交事务,线程安全),mode在windows中忽略
- if(db.open(NULL,"test.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"同名数据库存在"<<endl;
- }
游标的覆盖操作
- Db db(NULL,DB_CXX_NO_EXCEPTIONS);
- db.set_flags(DB_DUP);
- if(db.open(NULL,"DB\\test.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- int nkey=1;
- char szBuf[100]="愚蠢的地球人";
- Dbt key(&nkey,sizeof(int));
- Dbt value(szBuf,strlen(szBuf));
- for(int i=0;i<5;++i)
- {
- db.put(NULL,&key,&value,0);
- }
- Dbc *currsor=0;
- db.cursor(NULL,&currsor,0);
- Dbt key1;
- Dbt value1;
- sprintf(szBuf,"愚蠢的火星人");
- currsor->get(&key1,&value1,DB_FIRST);
- currsor->get(&key1,&value1,DB_NEXT);
- nkey=100;
- if(currsor->put(&key,&value,DB_CURRENT)!=0)
- {
- cout<<"put失败"<<endl;
- }
- currsor->get(&key1,&value1,DB_FIRST);
- cout<<*((int *)key1.get_data())<<":"<<(char*)value1.get_data()<<endl;
- while(currsor->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<*((int *)key1.get_data())<<":"<<(char*)value1.get_data()<<endl;
- }
二级数据库 没成功
- struct Info
- {
- char name[20];
- int age;
- };
- int Fun(Db *pSecondary,const Dbt *key,const Dbt *value,Dbt *result)
- {
- //result->set_flags(DB_DBT_APPMALLOC);
- Info *p=(Info *)value->get_data();
- result->set_data((void *)(p->age));
- result->set_size(sizeof(int));
- //result->set_ulen(4);
- return 0;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Db dbPrimary(NULL,DB_CXX_NO_EXCEPTIONS);
- if(dbPrimary.open(NULL,"DB\\Primary.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- Dbt key,value;
- int nID=0;
- Info info;
- key.set_data(&nID);
- key.set_size(sizeof(int));
- value.set_data(&info);
- value.set_size(sizeof(Info));
- int nReturn=0;
- nID=100;
- sprintf(info.name,"张三");
- info.age=20;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- nID=200;
- sprintf(info.name,"里斯");
- info.age=10;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- nID=300;
- sprintf(info.name,"周杰伦");
- info.age=40;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- if(nReturn!=0)
- {
- cout<<"出错啦"<<endl;
- return 0;
- }
- Db dbSecondary(NULL,DB_CXX_NO_EXCEPTIONS);
- if(dbSecondary.open(NULL,"DB\\Secondary.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- if(dbPrimary.associate(NULL,&dbSecondary,Fun,DB_CREATE)!=0)
- {
- cout<<"associate错误"<<endl;
- return 0;
- }
- /*Dbc *pCursor=0;
- if(dbSecondary.cursor(NULL,&pCursor,0)!=0)
- {
- cout<<"cursor创建错误"<<endl;
- return 0;
- }
- */
- nID=100;
- if(dbSecondary.get(NULL,&key,&value,0)==0)
- {
- cout<<((Info *)value.get_data())->name<<endl;
- }
- /*Dbt key1;
- Dbt value1;
- nID=20;
- if(pCursor->get(&key1,&value1,DB_FIRST|DB_GET_BOTH)!=0)
- {
- if(pCursor->get(&key1,&value1,DB_FIRST)==DB_NOTFOUND)
- cout<<"没有元素"<<endl;
- cout<<"get错误"<<endl;
- return 0;
- }
- cout<<*((int *)key1.get_data())<<":"<<((Info *)value1.get_data())->name<<endl;
- while(pCursor->get(&key1,&value1,DB_NEXT)!=0)
- {
- cout<<*((int *)key1.get_data())<<":"<<(char *)value1.get_data()<<endl;
- }*/
- return 0;
- }
部分写
- Db dbPrimary(NULL,DB_CXX_NO_EXCEPTIONS);
- if(dbPrimary.open(NULL,"DB\\Primary.db",NULL,DB_BTREE,DB_CREATE|DB_TRUNCATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- Dbt key,value;
- int nID=0;
- Info info;
- key.set_data(&nID);
- key.set_size(sizeof(int));
- value.set_data(&info);
- value.set_size(sizeof(Info));
- int nReturn=0;
- nID=100;
- sprintf(info.name,"张三");
- info.age=20;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- nID=200;
- sprintf(info.name,"里斯");
- info.age=10;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- nID=300;
- sprintf(info.name,"周杰伦");
- info.age=40;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- if(nReturn!=0)
- {
- cout<<"出错啦"<<endl;
- return 0;
- }
- nID=200;
- char name[20]={"四"};
- Dbt bt;
- bt.set_flags(DB_DBT_PARTIAL);
- bt.set_data(name);
- bt.set_size(20);
- bt.set_doff(0);
- bt.set_dlen(20);
- if(dbPrimary.put(NULL,&key,&bt,NULL)==0)
- {
- cout<<"成功"<<endl;
- Dbc *pCursor=0;
- dbPrimary.cursor(NULL,&pCursor,0);
- Dbt key1,value1;
- while(pCursor->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<*((int *)(key1.get_data()))<<":"<<((Info *)(value1.get_data()))->name<<":"<<((Info *)(value1.get_data()))->age<<endl;
- }
- }
事务
- DbEnv env(0);
- env.set_data_dir("Database");
- env.set_lg_dir("Log");
- //日志系统木默认是开启的
- env.open("Environment",DB_CREATE|DB_INIT_MPOOL|DB_INIT_TXN|DB_INIT_LOCK|DB_PRIVATE,0);
- Db dbPrimary(&env,DB_CXX_NO_EXCEPTIONS);
- DbTxn *pTxn;
- env.txn_begin(NULL,&pTxn,0);
- if(dbPrimary.open(pTxn,"Primary.db",NULL,DB_BTREE,DB_CREATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- pTxn->commit(0);
- Dbt key,value;
- int nID=0;
- Info info;
- key.set_data(&nID);
- key.set_size(sizeof(int));
- value.set_data(&info);
- value.set_size(sizeof(Info));
- int nReturn=0;
- nID=100;
- sprintf(info.name,"张三");
- info.age=20;
- nReturn+=dbPrimary.put(NULL,&key,&value,0);
- nID=200;
- sprintf(info.name,"里斯");
- info.age=10;
- int ntime1=timeGetTime();
- for(int i=0;i<1000000;++i)
- {
- nID+=1;
- env.txn_begin(NULL,&pTxn,0);
- dbPrimary.put(pTxn,&key,&value,0);
- pTxn->abort();
- }
- int ntime2=timeGetTime();
- cout<<ntime2-ntime1<<endl;
- env.txn_begin(NULL,&pTxn,0);
- nID=300;
- sprintf(info.name,"周杰伦");
- info.age=40;
- nReturn+=dbPrimary.put(pTxn,&key,&value,0);
- pTxn->abort();
- if(nReturn!=0)
- {
- cout<<"出错啦"<<endl;
- return 0;
- }
- Dbc *pCursor=0;
- dbPrimary.cursor(NULL,&pCursor,0);
- Dbt key1,value1;
- while(pCursor->get(&key1,&value1,DB_NEXT)==0)
- {
- cout<<*((int *)(key1.get_data()))<<":"<<((Info *)(value1.get_data()))->name<<":"<<((Info *)(value1.get_data()))->age<<endl;
- }
最终方案
- DbEnv env(0);
- env.set_data_dir("Database");
- env.set_lg_dir("Log");
- env.set_cachesize(0,500*1024*1024,0);
- env.log_set_config(DB_TXN_NOSYNC,1);
- env.set_lg_bsize(300*1024*1024);
- //日志系统木默认是开启的
- env.open("Environment",DB_CREATE|DB_INIT_MPOOL|DB_INIT_TXN|DB_INIT_LOCK|DB_PRIVATE|DB_RECOVER,0);
- //打开数据库
- Db dbPrimary(&env,DB_CXX_NO_EXCEPTIONS);
- DbTxn *pTxn;
- env.txn_begin(NULL,&pTxn,0);
- if(dbPrimary.open(pTxn,"Primary.db",NULL,DB_BTREE,DB_CREATE,0)!=0)
- {
- cout<<"打开错误"<<endl;
- return 0;
- }
- pTxn->commit(0);
- Dbt key,value;
- int nID=0;
- Info info;
- key.set_data(&nID);
- key.set_size(sizeof(int));
- value.set_data(&info);
- value.set_size(sizeof(Info));
- nID=200;
- sprintf(info.name,"里斯");
- info.age=10;
- int ntime1=timeGetTime();
- env.txn_begin(NULL,&pTxn,0);
- for(int i=0;i<20000;++i)
- {
- nID+=1;
- dbPrimary.put(pTxn,&key,&value,0);
- }
- pTxn->commit(0);
- int ntime2=timeGetTime();
- cout<<(ntime2-ntime1)/1000<<endl;
- system("pause");
一些配置
- DbEnv env(0);
- env.set_data_dir("Database");
- env.set_lg_dir("Log");
- env.set_cachesize(0,500*1024*1024,0);
- env.log_set_config(DB_TXN_NOSYNC,1);
- env.set_lg_bsize(300*1024*1024);
- //db_env->set_lg_max()
- //日志系统木默认是开启的
- env.open("Environment",DB_CREATE|DB_INIT_MPOOL|DB_INIT_TXN|DB_INIT_LOCK|DB_PRIVATE|DB_RECOVER,0);
- env.set_flags(DB_HOTBACKUP_IN_PROGRESS,1);
- env.txn_checkpoint(0,0,DB_FORCE);
- env.log_archive(NULL,DB_ARCH_REMOVE);
- env.log_set_config(DB_LOG_AUTO_REMOVE,1);
- env.txn_checkpoint(0,0,DB_FORCE);