The Header File
Code: Select all
#ifndef SQLMANAGER_H
#define SQLMANAGER_H
#define SQLITE_CORE
#include <irrlicht.h>
#include <irrarray.h>
extern "C" {
#include "../sqlite/sqlite3.h"
#include "../sqlite/sqlite3ext.h"
}
static irr::core::array<irr::core::stringc> result;
class SQLManager
{
private:
sqlite3 *db;
char *zErrMsg;
int rc;
//
public:
void init(irr::core::stringc fname);
void release();
irr::core::array<irr::core::stringc> Query(irr::core::stringc fname ,irr::core::stringc query);
};
#endif
Code: Select all
#include "SQLManager.h"
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
irr::core::stringc msg;
for(int i = 0; i < argc; i++)
{
msg += argv[i];
msg += " "; // space is the delimiter i use, feel free to change this.
}
result.push_front(msg);
return 0;
}
void SQLManager::init(irr::core::stringc fname)
{
rc = sqlite3_open(fname.c_str(), &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
}
}
void SQLManager::release()
{
sqlite3_close(db);
}
irr::core::array<irr::core::stringc> SQLManager::Query(irr::core::stringc fname , irr::core::stringc query)
{
init(fname);
result.clear();
rc = sqlite3_exec(db, query.c_str(), callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
release();
return result;
}
Code: Select all
// note result is the global variable i am using to retrieve the query results
result = pManager->SQLdb.Query("select name from profile");
// be cautious using this in update loop. make sure you call it once not each & every time else performance will suffer