Saving and calling resources of third-party libraries of cocos2dx sqlite
Sqlite3 is a simple front-end database. It is very convenient for games that store more data than before.
# Ifndef _ Sqlite3Test _ DataBaseHelper __
# Define _ Sqlite3Test _ DataBaseHelper __
# Include
# Include "sqlite3.h"
# Include
# Include
# Define DBNOTFOUND INT_MAX
Class DataBaseHelper
{// ===== Database operation handle ====
Public:
Static DataBaseHelper * sharedDataBaseHelper ();
~ DataBaseHelper ();
Int countForTable (const char * 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 DataBaseTable
{// Database table class
Protected:
DataBaseTable (){}
Virtual void parseStatement (sqlite3_stmt *) = 0;
Public:
Static T findDataById (int tid)
{// ===== Locate data by ID =======
Char condition [20];
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 (const char * tmp, int tid)
{
Char condition [30];
Sprintf (condition, "% s = % d", tmp, tid); // based on conditions
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)
{// Locate the data by ID and name
Char condition [30];
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;
}
// The vector container template obtains the data.
Static std: vector FindData (const char * condition = NULL, int offset = 0, int count = 0)
{
Std: vector 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 the database table
Return DataBaseHelper: sharedDataBaseHelper ()-> countForTable (T: tableName ());
}
};
# Endif/* defined (_ Sqlite3Test _ DataBaseHelper __)*/
# Include "DataBaseHelper. h"
# Include
# Include "../CCFileUtils. h"
# Include
# Include
# Include "cocos2d. h"
Using namespace std;
Using namespace cocos2d;
DataBaseHelper * DataBaseHelper: dataBaseHelper = NULL;
DataBaseHelper: DataBaseHelper ()
{
# If (CC_TARGET_PLATFORM = CC_PLATFORM_IOS)
// If you need to open the corresponding database path on 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 under the assets Directory. Therefore, copy the files to/data/package name/files/and perform subsequent operations.
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)
{
// If the database exists, do not 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: destroy ()
{
If (dataBaseHelper)
{
Delete dataBaseHelper;
}
}
DataBaseHelper ::~ DataBaseHelper ()
{
Sqlite3_close (database );
}
Int DataBaseHelper: countForTable (const char * table) {// number of returned tables
Char * SQL = (char *) malloc (strlen (table) + 22 );
Int count =-1;
Sprintf (SQL, "select count (*) from % s", table );
// The specific content of the sqlite3_stmt structure is not defined in sqlite. It is just an abstract type and is usually operated with its pointer during use,
// The sqlite3_stmt pointer is actually a pointer to the Vdbe struct.
Sqlite3_stmt * statement;
If (sqlite3_prepare_v2 (database, SQL,-1, & statement, NULL) = SQLITE_ OK) {// sqlite3_prepare_v2 query database interface
If (sqlite3_step (statement) = SQLITE_ROW)
{
Count = sqlite3_column_int (statement, 0 );
}
}
Free (SQL );
Return count;
}
// Query the table
Sqlite3_stmt * DataBaseHelper: queryTable (const char * 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 [20];
Sprintf (tmp, "% d, % d", offset, count );
SQL. append (tmp );
}
SQL. append (";");
// Sqlite3_stmt it is an internal data structure that has resolved SQL statements and marked records by sqlite itself.
Sqlite3_stmt * statement;
If (sqlite3_prepare_v2 (database, SQL. c_str (),-1, & statement, NULL) = SQLITE_ OK ){
Return statement;
}
Return NULL;
}