Mencoba SQLite3
Dulu saya sempat mencari-cari, ada nggak ya sebuah database engine yang bisa diakses secara lokal layaknya file?. Waktu itu saya cari-cari di Google ternyata saya nggak nemu. -mungkin belum beruntung atau kata kunci belum tepat -. :) . Kira2 sebulan yang lalu secara tidak sengaja saya nemu SQLite.DLL disebuah folder suatu game, saya langsung berfikir ini yang saya cari2 dulu!!!. Karena ada kata “SQL” dan “Lite” . Dan ternyata benar!, langsung ta download di official-webnya . Disitu bisa download .DLL -nya , source, semuanya.Untuk Sqlite3.lib tidak ada, dan bisa di-generate dengan:
lib /machine:i386 /def:sqlite3.def
lib.exe adalah bawaan Visual C++ (ada di dir-nya). Sqlite3 cukup mudah dipakai, namun API-nya memakai C murni. untuk mempermudah pemakaian saya mencoba membuat buat wrapper sederhana . saya post sourcecode-nya berikut ini:
Header:
[sourcecode language=“cpp”]
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "sqlite3.h"
/***
* Simple Sqllite3 Wrapper
* Edie @ Jan 12, 2011
*/
namespace SimDataBase
{
typedef std::map MapStrStr;
typedef std::pair PairStrStr;
template //delete inside vector
class Deleter
{
public:
void operator()(const T* it) const
{
delete it;
}
};
class DataResult
{
public:
DataResult(){};
~DataResult()
{
mMapStrStr.clear();
};
void AddData(const std::string& sVal1,const std::string& sVal2)
{
mMapStrStr.insert(PairStrStr(sVal1,sVal2));
}
void GetDataByFieldName(const std::string& fieldName,std::string& out)
{
MapStrStr::iterator it;
for(it = mMapStrStr.begin(); it!=mMapStrStr.end(); ++it)
{
if( fieldName == it->first)
{
out=it->second;
}
}
}
private:
MapStrStr mMapStrStr;
};
class DBHelper
{
public:
bool Open(const char* nameDB);
void Close();
void Exec(const char* sqlQuery);
unsigned int GetResultCount()const;
void GetResultVector(std::vector& outVec)const;
static DBHelper& Instance();
private:
int mResultCount;
bool mStatusOpen;
char* mErrMsg;
sqlite3* mDB;
std::vectormVecResult;
static DBHelper* mThis;
private:
DBHelper();
~DBHelper();
static int Callback(void *NotUsed, int argc, char **argv, char **azColName);
void ClearPrevResult();
};
}//end namespace SimDataBase
[/sourcecode]
Source:
[sourcecode language=“cpp”]
#include "DBHelper.h"
#include <algorithm>//untuk std::for_each
DBHelper* DBHelper::mThis;
DBHelper::DBHelper()
{
mErrMsg=0;
mResultCount=0;
}
DBHelper::~DBHelper()
{
}
bool DBHelper::Open(const char* nameDB)
{
int res;
res=sqlite3_open(nameDB, &mDB);
if(0==res)
{
mStatusOpen=true;
return 1;
}
else
{
std::cout<<"Failed open database. Error code: "<<
mStatusOpen=false;
return 0;
}
}
void DBHelper::Close()
{
sqlite3_close(mDB);
mStatusOpen=false;
}
void DBHelper::Exec(const char* sqlQuery)
{
ClearPrevResult();
mResultCount=0;
int res=sqlite3_exec(mDB,sqlQuery,&DBHelper::Callback,0,&mErrMsg);
if(res!=SQLITE_OK)
{
std::cout<<"Failed open database. Error code: "<<res<<std::endl;
mResultCount=0;
}
}
unsigned int DBHelper::GetResultCount()const
{
return mResultCount;
}
int DBHelper::Callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
NotUsed=0;
DataResult* pDR=new DataResult();
for(i=0; i<argc; i++)
{
pDR->AddData(azColName[i],argv[i]);
}
mThis->mVecResult.push_back(pDR);
mThis->mResultCount=mThis->mResultCount+1;
std::cout<<"Result :"<<mThis->mVecResult.size()<<std::endl;
return 0;
}
DBHelper& DBHelper::Instance()
{
if(0==mThis)
{
mThis=new DBHelper();
}
return *mThis;
}
void DBHelper::ClearPrevResult()
{
if(!mVecResult.empty())
{
std::for_each(mVecResult.begin(),mVecResult.end(),Deleter());
mVecResult.clear();
}
}
[/sourcecode]
Contoh pemakaian:
[sourcecode language=“cpp”]
<pre>#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#include <iostream>
#include <string>
#include <vector>
#include "DBHelper.h"
using namespace SimDataBase;
int main(int argc, char **argv)
{
char* dbname="Data.db";//nama database
if(!DBHelper::Instance().Open(dbname))
{
std::cout<<"error: database couldn't opened"<
exit(1);
}
char* query2="select NAME,SPEED,HEIGHT,RUDDER,THROTTLE from shiptable";
DBHelper::Instance().Exec(query2);
std::vectorres;
std::vector::iterator it;
DBHelper::Instance().GetResultVector(res);
for(it=res.begin();it!=res.end();it++)
{
std::string s;
(*it)->GetDataByFieldName("NAME",s);
std::cout<<"Name: "<<GetDataByFieldName("SPEED",s);
std::cout<<"Speed: "<<
std::cout<<"======="<
}
DBHelper::Instance().Close();
return 0;
}
[/sourcecode]
Download Source Code Here. (sqlite3.lib & sqlite3.dll included)
keyword: database c++, koneksi database c++, sqlite3, query