1.源码实现
#include <iostream>
#include <leveldb/db.h>
using namespace std;
int main()
{
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
string dbpath = "./leveldb.db";
leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);
if(status.ok() == false)
{
cout << "leveldb open failed" << endl;
return -1;
}
string key = "petter";
string value = "good to see you ";
cout << "Open db ok" << endl;
string result;
status = db->Put(leveldb::WriteOptions(), key, value);
if(status.ok() == false)
{
cout << "leveldb write failed " << endl;
delete db;
return -1;
}
cout << "insert successful" << endl;
status = db->Get(leveldb::ReadOptions(), key, &result);
if(status.ok() == false)
{
cout << "leveldb read failed" << endl;
delete db;
return -1;
}
cout << "result: " << result << endl;
delete db;
return 0;
}
2.编译源码
$ g++ -o test test.cpp -std=c++11 -lleveldb
3.运行及其结果
$ ./test
Open db ok
insert successful
result: good to see you
网友评论