Copy and paste from what I currently use. Edit: forgot to post how to get data in.
put into database
bool EntityFactory::saveEntitiesDB3(std::string str) {
sqlite3 * db = NULL;
int result = 0;
result = sqlite3_open(str.data(), &db);
if (result != SQLITE_OK) {
sqlite3_close(db);
return false;
}
bool dbClean = true;
sqlite3_stmt *pStmt = NULL;
result = sqlite3_exec(db, "DELETE FROM entities;", NULL, NULL, NULL); // clear out old save data
result = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL); // begin a transaction for efficiency and speed
std::string sqlTxt("INSERT INTO entities (ownerid,type,skills) VALUES (?1,?2,?3)");
result = sqlite3_prepare_v2(db, sqlTxt.data(), sqlTxt.size(), &pStmt, NULL);
if (result == SQLITE_OK && pStmt) {
std::vector<Entity*>::iterator vitEntity = Entities.begin();
Entity * pE = NULL;
std::string const * eName = NULL;
std::vector<Skill> const * vSkills = NULL;
while (vitEntity != Entities.end()) {
pE = (*vitEntity++); // point to the entity(npc/pc) in the list
// bind all the variables into the above SQL statement
sqlite3_bind_int(pStmt, 1, pE->getID());
eName = pE->getTypeName();
sqlite3_bind_text(pStmt, 2, eName->data(), eName->size(), NULL);
vSkills = pE->getSkillsList();
std::size_t size = vSkills->size() * sizeof (Skill);
sqlite3_bind_blob(pStmt, 3, vSkills->data(), size, SQLITE_STATIC); // sqlite_static is important, so object is not destructed!
sqlite3_step(pStmt);
sqlite3_reset(pStmt);
}
} else {
dbClean = false;
}
result = sqlite3_finalize(pStmt);
if (dbClean) result = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL); // commits transaction if clean
sqlite3_close(db);
return dbClean;
}
read from database
bool EntityFactory::loadEntitiesFromDB(std::string str) {
sqlite3 * db = NULL;
int result = 0;
result = sqlite3_open(str.data(), &db);
if (result != SQLITE_OK) {
sqlite3_close(db);
return false;
}
bool dbClean = true;
std::string sqlTxt("SELECT ownerid,typename,skills FROM entities;");
sqlite3_stmt *pStmt = NULL;
result = sqlite3_prepare_v2(db, sqlTxt.data(), sqlTxt.size(), &pStmt, NULL);
if (result == SQLITE_OK && pStmt) {
while (sqlite3_step(pStmt) == SQLITE_ROW) {
/// grab values from rowset here
int i = sqlite3_column_int(pStmt, 0);
std::string str(reinterpret_cast<const char*> (sqlite3_column_text(pStmt, 1)));
// point at the beginning of the data blob with a class pointer.
// skill in this case.
Skill const * pSK = (Skill const*) sqlite3_column_blob(pStmt, 2);
// grab blob size. It was
int size = sqlite3_column_bytes(pStmt, 2);
// create vector using pointer and memory size,
// it is smart enough to chop up the blob into bite size classes
std::vector<Skill> vSkills(pSK, pSK + size);
// create new entity from save information,
// tosses into entity list when done.
createEntity(str, i, vSkills);
}
} else {
dbClean = false;
}
sqlite3_finalize(pStmt);
sqlite3_close(db);
return dbClean;
}
If you need the compiled library and libs, I have a copy on bitbucket.
https://bitbucket.org/willraman/legacy-of-a-warlord-client/I'd recommend getting the source from the source though,
http://www.sqlite.org/download.html