zoukankan      html  css  js  c++  java
  • 用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下就会报错。

    .pro文件需要加上这两行:

    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 这样的参数的问题了,只需要这样:

    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)
  • 相关阅读:
    LeetCode子集问题
    面试题-求最大字典区间
    链表快速排序
    树的非递归遍历
    快速排序非递归实现
    leetcode217 python3 72ms 存在重复元素
    leetcode121 C++ 12ms 买股票的最佳时机 只能买卖一次
    leetcode1 python3 76ms twoSum 360面试题
    leetcode485 python3 88ms 最大连续1的个数
    leetcode119 C++ 0ms 杨辉三角2
  • 原文地址:https://www.cnblogs.com/afxcn/p/3998374.html
Copyright © 2011-2022 走看看