#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文件需要加上这两行:
LIBS += -L/opt/local/lib/ -lleveldb
INCLUDEPATH += /opt/local/include