用Qt Creator 對 leveldb 進行簡單的讀寫

#include <iostream>
#include <string>
#include <leveldb/db.h>
#include <boost/lexical_cast.hpp>

using namespace std;

int main(int argc, char *const *argv)
{

    try
    {
        leveldb::DB* db;
        leveldb::Options options;
        options.create_if_missing = true;
        leveldb::Status status = leveldb::DB::Open(options, "./db", &db);

        cout << status.ok() << endl;

        for(int i=0; i<99999; i++)
        {
            string key = boost::lexical_cast<string>(i);
            string value = boost::lexical_cast<string>(i*i);
            db->Put(leveldb::WriteOptions(), key, value);
        }

        string key = "666";
        string value;

        db->Get(leveldb::ReadOptions(), key, &value);

        cout << value << endl;
        cout << 666*666 << endl;

        delete db;
    }
    catch(exception &e)
    {
        cout << e.what() << endl;
    }

    return 0;
}

代碼沒什麼特點,由於Qt Creator在編譯的時候會加上 -mmacosx-version-min=10.6 這樣的參數,因此咱們在編譯libleveldb.a的時候也須要加上這個參數,不然在Qt下就會報錯。ios

.pro文件須要加上這兩行:macos

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH += /opt/local/include
LIBS += -L/opt/local/lib/ -lleveldb

 

若是是用cmake,那就沒有 -mmacosx-version-min=10.6 這樣的參數的問題了,只須要這樣:app

project(hellodb)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

INCLUDE_DIRECTORIES(/opt/local/include)
TARGET_LINK_LIBRARIES (hellodb /opt/local/lib/libleveldb.a)
相關文章
相關標籤/搜索