LevelDB数据库使用
2011-12-24 15:31
190 查看
向数据库插入数据的操作如下:
leveldb::DB* LocalCacheDB:: m_pDB=NULL;
leveldb::Options LocalCacheDB:: m_options;
bool WriteToDB(INFO& info)
{
leveldb::WriteOptions wo;
leveldb::ReadOptions ro;
wo.sync = false;//考虑是否异步
//将info转为char[]
int nLen = sizeof(INFO);
char* chTmp = new char[nLen];
ZeroMemory(chTmp, nLen);
memcpy(chTmp,&info, nLen);
char chKey[10];
ZeroMemory(chKey, 10);
leveldb::Iterator *iter = m_pDB->NewIterator(ro);
if (iter == NULL)
{
return false;
}
iter->SeekToLast();//last的key最大,这里是自己定义的比较函数按key排序
if (!iter->Valid())
{
//数据库为空
itoa(1, chKey, 10);//key从1开始
}
else
{
leveldb::Slice sliceKey;
sliceKey=iter->key();
HPR_INT32 nKey=0;
nKey=atoi(sliceKey.data());
nKey++;
itoa(nKey,chKey,10);
}
delete iter;//切忌在使用完sliceKey之前删除iter
leveldb::Status s = m_pDB->Put(wo,chKey,leveldb::Slice(chTmp,nLen));
delete[] chTmp;
if (!s.ok())
{
return false;
}
cout<<"存入一条数据 index:"<<chKey<<endl;
return true;
}
//比较函数如下:
class DBComparator:public leveldb::Comparator
{
public:
int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const
{
int na,nb;
na=atoi(a.data());
nb=atoi(b.data());
if (na<nb)
{
return -1;
}
if (na>nb)
{
return +1;
}
return 0;
}
// Ignore the following methods for now:
const char* Name() const { return "DBComparator"; }
void FindShortestSeparator(std::string*, const leveldb::Slice&) const { }
void FindShortSuccessor(std::string*) const { }
};
leveldb::DB* LocalCacheDB:: m_pDB=NULL;
leveldb::Options LocalCacheDB:: m_options;
bool WriteToDB(INFO& info)
{
leveldb::WriteOptions wo;
leveldb::ReadOptions ro;
wo.sync = false;//考虑是否异步
//将info转为char[]
int nLen = sizeof(INFO);
char* chTmp = new char[nLen];
ZeroMemory(chTmp, nLen);
memcpy(chTmp,&info, nLen);
char chKey[10];
ZeroMemory(chKey, 10);
leveldb::Iterator *iter = m_pDB->NewIterator(ro);
if (iter == NULL)
{
return false;
}
iter->SeekToLast();//last的key最大,这里是自己定义的比较函数按key排序
if (!iter->Valid())
{
//数据库为空
itoa(1, chKey, 10);//key从1开始
}
else
{
leveldb::Slice sliceKey;
sliceKey=iter->key();
HPR_INT32 nKey=0;
nKey=atoi(sliceKey.data());
nKey++;
itoa(nKey,chKey,10);
}
delete iter;//切忌在使用完sliceKey之前删除iter
leveldb::Status s = m_pDB->Put(wo,chKey,leveldb::Slice(chTmp,nLen));
delete[] chTmp;
if (!s.ok())
{
return false;
}
cout<<"存入一条数据 index:"<<chKey<<endl;
return true;
}
//比较函数如下:
class DBComparator:public leveldb::Comparator
{
public:
int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const
{
int na,nb;
na=atoi(a.data());
nb=atoi(b.data());
if (na<nb)
{
return -1;
}
if (na>nb)
{
return +1;
}
return 0;
}
// Ignore the following methods for now:
const char* Name() const { return "DBComparator"; }
void FindShortestSeparator(std::string*, const leveldb::Slice&) const { }
void FindShortSuccessor(std::string*) const { }
};
相关文章推荐
- 比特币客户端使用的数据库levelDB介绍
- Qt 学习之路 2(56):使用模型操作数据库
- iOS中使用FMDB对数据库的操作(一)
- 使用客户数据集获取数据库服务器信息
- 开源框架-ormlite-android数据库的使用
- 创建3台虚拟主机,安装论坛,每个论坛使用不同的数据库
- 使用JDBC连接数据库 查询数据库中内容 简单实例
- 关于Android的轻量级数据库SQLlite的使用实践小项目 (生管理系统(附源码))
- WSS 数据库表中的 UserInfo 表中的 tp_SystemId 字段的使用
- 数据库数据被改,使用log explorder查询修改记录
- django 使用 oracle 数据库的问题
- ORACLE 存储过程 使用DBLINK 连接远程数据库
- 在跨平台的C++程序中使用OTL来操作不同的数据库
- Android数据库高手秘籍(三)——使用LitePal升级表
- iOS 使用FMDB进行数据库操作
- Spring Security教程(11)---- 使用数据库来管理资源
- 使用PD(PowerDesigner)图如何快速生成创建数据库表的SQL脚本
- 在Asp中使用断开式数据库连接
- 使用Visual Basic访问数据库几个注意的问题
- asterisk 相关数据库配置 使用