leveldb实例

来源:互联网 发布:Sql中not in的效率 编辑:程序博客网 时间:2024/04/30 01:59
  1. #include <iostream>  
  2. #include "db.h"  
  3. #include "iterator.h"  
  4. #include <stdio.h>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     leveldb::DB* db;    
  10.     leveldb::Options options;    
  11.     options.create_if_missing = true;    
  12.     std::string dbpath = "testdb";    
  13.     leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);    
  14.     assert(status.ok());    
  15.     std::string key1 = "lyc";    
  16.     std::string value1 = "liyc7711@gamil.com";    
  17.     //cout<<"Open db OK"<<std::endl;    
  18.     string key2 = "haha";  
  19.     string value2 = "segfwaga";  
  20.   
  21.     std::string v1 = "";    
  22.     string v2 = "";  
  23.     leveldb::Status s ;    
  24.     s = db->Put(leveldb::WriteOptions(), key1, value1);/*key1和key2作为一对key-value对插入*/    
  25.     s = db->Get(leveldb::ReadOptions(), key1, &v1);/*根据key返回对应的value值*/    
  26.     db->Put(leveldb::WriteOptions(), key2, value2);  
  27.     db->Get(leveldb::ReadOptions(), key2, &v2);  
  28.     cout<<v1<< " "<< v2<<std::endl;   
  29.     leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());  
  30.     for(it->SeekToFirst(); it->Valid(); it->Next()){  
  31.        cout<< it->key().ToString() << ":" << it->value().ToString()<<endl;  
  32.     }  
  33.     delete it;   
  34.     delete db;/*删除数据库*/    
  35. }  
0 0
原创粉丝点击