美文网首页
c++操作leveldb数据库

c++操作leveldb数据库

作者: 一路向后 | 来源:发表于2022-02-27 21:12 被阅读0次

    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 
    

    相关文章

      网友评论

          本文标题:c++操作leveldb数据库

          本文链接:https://www.haomeiwen.com/subject/jqqkrrtx.html