您的位置:首页 > 数据库

iOS 轻量级的数据库leveldb

2015-12-08 21:23 162 查看
轻量级的数据库leveldb https://github.com/google/leveldb

一:在iOS下编译leveldb

终端:

1: git clone https://github.com/google/leveldb.git
2: 进入根目录

cd leveldb-master

3: 编译:

CXXFLAGS=-stdlib=libc++ make PLATFORM=IOS

编译完成之后,在当前目录里会生成 libleveldb.a , 支持全部模拟器与真机;

头文件在 include 目录下

二:引用库

leveldb是c++的;使用的时候需要oc的.m文件修改为.mm文件;

将编译好的库以group的形式导入到xcode工程;

使用oc的方式引用

#import "db.h"

#import "options.h"

#import "write_batch.h"

这时会出现xxx.h找不到的错误;

直接修改include 目录下的头文件;

把出错的头文件部分做如下修改

这种#include "leveldb/xx.h" 改为#include "xx.h"

三:test

#include <iostream>
#include <string>
#include <assert.h>
#include "leveldb/db.h"

using namespace std;

int main(void)
{

leveldb::DB      *db;
leveldb::Options  options;
options.create_if_missing = true;

// open
leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
assert(status.ok());

string key = "name";
string value = "chenqi";

// write
status = db->Put(leveldb::WriteOptions(), key, value);
assert(status.ok());

// read
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());

cout<<value<<endl;

// delete
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());

status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) {
cerr<<key<<"    "<<status.ToString()<<endl;
} else {
cout<<key<<"==="<<value<<endl;
}

// close
delete db;

return 0;
}


  

参考:http://www.tanhao.me/pieces/1397.html/
http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: