怎样操作leveldb数据库,实现增删改查

2024-11-24 21:19:32
推荐回答(1个)
回答1:

怎样操作leveldb数据库,实现增删改查
#include
#include
#include
#include "leveldb/db.h"

using namespace std;

int main(void)
{

leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;

// open
leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
assert(status.ok());

string key = "name";
string value = "chenqi";

// write
status = db->Put(leveldb::WriteOptions(), key, value);
assert(status.ok());

// read
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());

cout<
// delete
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());

status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) {
cerr<} else {
cout<}

// close
delete db;

return 0;
}