cocos2dx sqlite第三方库的资源保存和调用

sqlite3是一个简单的前端数据库,对于一些动作和前段保存数据比较多的游戏使用还是很方便

#ifndef __Sqlite3Test__DataBaseHelper__

#define __Sqlite3Test__DataBaseHelper__

#include <iostream>

#include "sqlite3.h"

#include <vector>

#include <string>

#define DBNOTFOUND INT_MAX

class DataBaseHelper

{     //=====数据库操作手柄======

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 T>

class DataBaseTable

{   //数据库表类

protected:

DataBaseTable(){}

virtual void parseStatement(sqlite3_stmt *) =
0;

public:

static T findDataById(int tid)

{    //=====根据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); 
//根据条件判断

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)

{ //根据ID和名字找到数据

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;

}

//vector容器模板拿到数据

static std::vector<T> findData(const
char *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()

{      //返回数据库表的大小

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::dataBaseHelper = NULL;

DataBaseHelper::DataBaseHelper()

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

//如果在IOS平台需要打开相应的数据库路径

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()

{

// android 系统不能对assets目录下的文件进行fopen操作,所以copy到 /data/data/包名/files/
下面再操作

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)

{

// 数据库存在的话就别再copy过去了

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) {  //返回表的数量

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

int count = -1;

sprintf(sql, "select count(*) from %s", table);

// 在sqlite中并没有定义sqlite3_stmt这个结构的具体内容,它只是一个抽象类型,在使用过程中一般以它的指针进行操作,

//而sqlite3_stmt类型的指针在实际上是一个指向Vdbe的结构体得指针

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(database, sql, -1, &statement,
NULL)==SQLITE_OK) {  //sqlite3_prepare_v2 
查询数据库接口

if (sqlite3_step(statement)==SQLITE_ROW)

{

count = sqlite3_column_int(statement,
0);

}

}

free(sql);

return count;

}

//查询表

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   它是一个已经把sql语句解析了的、用sqlite自己标记记录的内部数据结构。

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(database, sql.c_str(), -1, &statement,
NULL)==SQLITE_OK) {

return statement;

}

return NULL;

}

时间: 2024-08-25 07:10:03

cocos2dx sqlite第三方库的资源保存和调用的相关文章

第三方SQLITE打包库pldatabase的介绍

1. 怎么导入PlausibleDatabase.framework框架 先下载PlausibleDatabase.framework框架包,然后把该包直接加入到库中,然后再加入#import <PlausibleDatabase/PlausibleDatabase.h>头文件 下载地址:http://code.google.com/p/pldatabase/ 在这里可以下载和查看文档和代码. 第三方SQLITE封装库Pldatabase 基本使用指南 创建一个链接 为存在数据库文件打开一个链

mac下cocos2dx(带jsoncpp第三方库)编译为android项目心得

在mac下好不容写完了游戏,本以为轻轻松松就能编译为android项目,想不到弄了将近1整天才将问题解决 首先不带jsoncpp的编译方式请参考:http://www.bold-it.com/ios/cocos2d-x-box2d-iosandroid-hybrid-tutorial/ 带jsoncpp第三方库的参考了http://blog.csdn.net/sniffer12345/article/details/7336732,对作者表示感谢. 我使用的是正常的jsoncpp版本,在json

调用第三方库出现的问题

运行一个需要调用第三方库的小程序本应该是很简单的事情,但是要想顺利地把程序跑起来首先要迈的第一道坎就是正确的配置路径和编译环境.鉴于前两次在开始调用时,状况百出而我却无从下手,所以非常有必要回顾一下最基本的问题. 在windows 环境下,从编写到运行需要两个步骤:① 首先写好源文件(.c),经过cl.exe编译器编译,从而生成了.obj 目标文件[编译] ② 生成的目标文件(.obj)再结和库文件(.lib)经过link.exe 链接器从而生成可执行文件.[链接][运行]*.h   *.lib

android调用第三方库——第一篇 (转载)

转自:http://blog.csdn.net/jiuyueguang/article/details/9447245 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言: 这两天一直在研究用android的jni调用第三方库,上网搜方法,但是都是泛泛而谈,没有demo,经过我几番折磨,写了n多的 helloword工程,总是不成功,工程名字也就由helloowrd转到shithelloword再转到fuckhelloword再转到 bitchhelloword再转到ganhello

VS中使用第三方库原理(配置sqlite数据库)

我们在编写程序时,,不可避免的会使用第三方的库文件,很少使用源文件(.cpp),大部分是使用对类进行声明的头文件和封装了类的链接库(静态lib或动态dll),比如我们写程序用的iostream这个库,当然这个是已经设置好的官方的库,让我们使用某个第三方的库时,需要自己来进行配置. C++的库会把函数.类的声明放在*.h中,实现放在*.cpp或*.cc中.编译之后,*.cpp,*.cc,*.c会被打包成一个.lib文件,这样可以保护源代码. 所以,要使用一个库,除了要include他的头文件以外,

cocos2d-x 3.0 引用第三方库 及编译成apk时android mk文件写法

cocos2d-x 3.0 中,如果你需要使用CocosStudio.Extensions扩展库 等等,都需要自己手动添加. 添加步骤如下:(比如说现在我要添加libExtensions,libCocosStudio , libGUI) 1.添加附加项目 右击解决方案--->添加现有项目--->添加:项目目录\cocos2d\extensions\proj.win32\libExtensions,然后你会发现左边多了一个libExtensions扩展库 右击解决方案--->添加现有项目-

Android JNI如何调用第三方库

http://www.2cto.com/kf/201504/388764.html Android JNI找不到第三方库的解决方案 cannot load library 最近做一个jni项目,拿到的so库需要用jni封装一层,等于是在jni的C++代码里调用第三方库的方法,然后整个项目在Android上运行出结果. 自己用jni生成的so是libaa.so 使用的第三方库是libbb.so. 到目前为止,遇到的问题是libbb各种找不到.libbb库去哪儿了? E/AndroidRuntime

小程序云开发调用HTTP请求中got第三方库使用失败解决方法

小程序云开发调用HTTP请求中got第三方库使用失败解决方法 错误代码 {"errorCode":1,"errorMessage":"user code exception caught","stackTrace":"The \"original\" argument must be of type function"} 替换方案 // 云函数入口文件 const cloud = req

数据库使用之第三方库 FMDB

下载 FMDB 1. 引进 sqlite3 工具箱,在要进行数据库操作的类里引进头文件 : 因为第三方软件同样是使用  sqlite 工具箱来操作数据库的,只不过是简化了操作,让语法更接近 OC 的语法, 而不需要使用过多的 C 语法: #import <sqlite3.h> 2. 将第三方库加载进工程:方法是直接将 FMDB 的源文件拖拽进工程即可: 3. 使用第三方库访问数据库 当然了,对于高手而言,对第三方库进行了解后,上手是很快的,对于小白,只能一步一步走啦. 3.1 指定数据库的存储