COCOS2DX SQLite third-party library resource saving and invocation

Source: Internet
Author: User
Tags sprintf

Sqlite3 is a simple front-end database, for some actions and the previous period to save more data game use is still very convenient



#ifndef __sqlite3test__databasehelper__

#define __SQLITE3TEST__DATABASEHELPER__


#include <iostream>


#include "Sqlite3.h"

#include <vector>

#include <string>


#define Dbnotfound Int_max


class Databasehelper

{ //===== database operation handle ======

Public:

Static databasehelper* Shareddatabasehelper ();

~databasehelper ();

int countfortable (constchar * table);

SQLITE3_STMT * QueryTable (const char * table, const char *fields,const Char *condition, int offset=0, int count=0);

Static void destroy ();

void opensqliteinandroid ();

Private:

Databasehelper ();

Static Databasehelper *databasehelper;

Sqlite3 *database;

};



Template <class t>

class Databasetable

{ // database table class

Protected:

Databasetable () {}

Virtual void parsestatement (sqlite3_stmt *) =0;

Public:

Static T Finddatabyid (int tid)

{ //===== finds data based on ID =======

Char condition[];

sprintf (condition, "id=%d", Tid);

sqlite3_stmt * stmt = Databasehelper::shareddatabasehelper ()->querytable (T::tablename (),NULL, condition ,0,1);

T T;

t._id = Dbnotfound;

if ((Sqlite3_step (stmt) ==sqlite_row))

{

T.parsestatement (stmt);

}

Sqlite3_finalize (stmt);

return t;

}

Static T finddatabytmp (constchar *tmp,int tid)

{

Char condition[[];

sprintf (condition, "%s=%d", Tmp,tid); // Judging by condition

sqlite3_stmt * stmt = Databasehelper::shareddatabasehelper ()->querytable (T::tablename (),NULL, condition );

T T;

//t._id = Dbnotfound;

if ((Sqlite3_step (stmt) ==sqlite_row))

{

T.parsestatement (stmt);

}

Sqlite3_finalize (stmt);

return t;

}

Static T finddatabyidandname (int tid,const Char* name)

{ //Find Data by ID and name

Char condition[[];

sprintf (condition, "id=%d and Name= '%s '", Tid, name);

sqlite3_stmt * stmt = Databasehelper::shareddatabasehelper ()->querytable (T::tablename (),NULL, condition );

T T;

t._id = Dbnotfound;

if ((Sqlite3_step (stmt) ==sqlite_row))

{

T.parsestatement (stmt);

}

Sqlite3_finalize (stmt);

return t;

}

//vector container template to get the data

static std::vector<t> findData (constchar *condition=NULL, int offset=0, int count=0)

{

std::vector<t> Res;

sqlite3_stmt * stmt = Databasehelper::shareddatabasehelper ()->querytable (T::tablename (),NULL, condition );

while ((Sqlite3_step (stmt) ==sqlite_row))

{

T T;

T.parsestatement (stmt);

Res.push_back (t);

}

Sqlite3_finalize (stmt);

return Res;

}

Static int count ()

{ // Returns the size of a database table

return Databasehelper::shareddatabasehelper ()->countfortable (T::tablename ());

}

};



#endif /* Defined (__sqlite3test__databasehelper__) */





#include "DataBaseHelper.h"

#include <string>

#include ". /ccfileutils.h "

#include <stdlib.h>

#include <stdio.h>

#include "Cocos2d.h"



Using namespace std;

Using namespace cocos2d;


Databasehelper *databasehelper::d atabasehelper = NULL;


Databasehelper::D atabasehelper ()

{

#if (Cc_target_platform = = Cc_platform_ios)

// If you need to open the appropriate database path on the IOS platform

std::string path = Cocos2d::ccfileutils::sharedfileutils ()->fullpathforfilename ("Data/gamedatabean.db" );

Cclog ("------%s-------------", Path.c_str ());

int res = Sqlite3_open (Path.c_str (), &database);

if (res! = SQLITE_OK)

{

Cclog ("-->>open db fail,error code is%d", res);

}

#elif (Cc_target_platform = = cc_platform_android)

Opensqliteinandroid ();

#endif

}


void Databasehelper::opensqliteinandroid ()

{

//The Android system cannot perform fopen operations on files in the assets directory , so copy to /data/data/ package name /files/the operation below

std::string path = Cocos2d::ccfileutils::sharedfileutils ()->fullpathforfilename ("Data/gamedatabean.db" );

std::string Writalepath = Ccfileutils::sharedfileutils ()->getwritablepath () +"gamedatabean.db";

unsigned long len =0;

unsigned char *data =NULL;

data = Ccfileutils::sharedfileutils ()->getfiledata (Path.c_str (),"R", &len);

FILE *FP = fopen (Writalepath.c_str (),"R");

if (!FP)

{

// database exists then don't copy it .

FILE *FP1 = fopen (Writalepath.c_str (), "w+");

Fwrite (data, sizeof(char), Len, FP1);

Fclose (FP1);

} Else{

Fclose (FP);

}

int res = Sqlite3_open (Writalepath.c_str (), &database);

if (res! = SQLITE_OK)

{

Cclog ("-->>open db fail,error code is%d", res);

}

}


databasehelper* Databasehelper::shareddatabasehelper () {

if (!databasehelper) {

Databasehelper = new databasehelper ();

:: Atexit (Destroy);

}

return Databasehelper;

}


void Databasehelper::d Estroy ()

{

if (Databasehelper)

{

Delete databasehelper;

}

}


Databasehelper::~databasehelper ()

{

Sqlite3_close (database);

}

int Databasehelper::countfortable (constchar * table) { // Returns the number of tables

char *sql = (char *) malloc (strlen (table) +);

int count =-1;

sprintf (SQL, "SELECT COUNT (*) from%s", table);

// in SQLite , there is no specific content of the sqlite3_stmt structure, it is an abstract type, which is usually manipulated by its pointers during use.

the sqlite3_stmt type pointer is actually a pointer to the VDBE structure

Sqlite3_stmt *statement;

if (SQLITE3_PREPARE_V2 (Database, SQL,-1, &statement,NULL) ==SQLITE_OK) { //sqlite3_prepare_v2 querying the Database interface

if (Sqlite3_step (statement) ==sqlite_row)

{

Count = Sqlite3_column_int (statement, 0);

}

}

free (SQL);

return count;

}


// Enquiry Form

SQLITE3_STMT * databasehelper::querytable (constchar * table, const char *fields, Const Char *condition,int offset, int count)

{

String sql = string ("select");

if (Fields) {

Sql.append (fields);

} Else {

Sql.append ("*");

}

Sql.append ("from");

Sql.append (table);

if (condition) {

Sql.append ("where");

Sql.append (condition);

}

if (count)

{

Sql.append ("Limit");

Char tmp[];

sprintf (TMP, "%d,%d", offset, count);

Sql.append (TMP);

}

Sql.append (";");

//Sqlite3_stmt It is an internal data structure that has been parsed by SQL statements and recorded with SQLite 's own tag.

Sqlite3_stmt *statement;

if (SQLITE3_PREPARE_V2 (Database, Sql.c_str (),-1, &statement,NULL) ==SQLITE_OK) {

return statement;

}

return NULL;

}


Cocos2dx sqlite third-party library resource saving and invocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.