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);