【Cocos2d-x】集成wxSqlite3到Cocos2d-x

时间:2021-05-08 23:03:02


关于wxSqlite3


wxSqlite3是一个轻量级的开源数据库项目,基于sqlite3,相比sqlite3增加了数据库加密功能,是用C语言实现的,跨平台。


下载wxsqlite3


下载地址1:http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/

下载地址2:http://pan.baidu.com/s/11wzpk


集成wxsqlite3到Cocos2d-x


1.在cocos2d-x引擎external目录下新建wxsqlite3\src目录


2.把wxsqlite3-3.1.1\sqlite3\secure\src目录下的.h和.c文件拷贝到wxsqlite3/src目录下

【Cocos2d-x】集成wxSqlite3到Cocos2d-x


3.添加sqltesecure.c到libLocalStorage项目中。(Cocos2d-x2.x是添加到libExtensions项目)

【Cocos2d-x】集成wxSqlite3到Cocos2d-x


4.设置预编译宏:SQLITE_HAS_CODEC,该宏用于开启加密功能。

【Cocos2d-x】集成wxSqlite3到Cocos2d-x


5.相关api


(1). 设置密码(在sqlite3_open和sqlite3_close之间任何地方使用)

int sqlite3_key(
  sqlite3 *db,       //数据库引用
  const void *pKey,  //密码
  int nKey           //密码长度
);


(2). 修改密码(在sqlite3_open和sqlite3_close之间任何地方使用)

int sqlite3_rekey(
  sqlite3 *db,               //数据库引用
  const void *pKey,          //新密码
  int nKey                   //密码长度
);


示例:在LocalStorage.cpp中的localStorageInit方法中初始化数据库时设置密码

void localStorageInit( const char *fullpath)
{
if( ! _initialized ) {

int ret = 0;

if (!fullpath)
ret = sqlite3_open(":memory:",&_db);
else
ret = sqlite3_open(fullpath, &_db);

// 设置密码
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
sqlite3_key(_db,"test",4);
#endif

localStorageCreateTable();

// SELECT
const char *sql_select = "SELECT value FROM data WHERE key=?;";
ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, NULL);

// REPLACE
const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, NULL);

// DELETE
const char *sql_remove = "DELETE FROM data WHERE key=?;";
ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, NULL);

if( ret != SQLITE_OK ) {
printf("Error initializing DB\n");
// report error
}

_initialized = 1;
}
}


测试工程svn地址:http://code.taobao.org/svn/wxsqlite3_cocos2dx/trunk


编译到Android


1.在external\wxsqlite目录下添加Android.mk文件,文件内容如下:

LOCAL_PATH := $(call my-dir)

#清理变量定义
include $(CLEAR_VARS)

#模块名称
LOCAL_MODULE := wxsqlite3_static

#库文件名称
LOCAL_MODULE_FILENAME := libwxsqlite3

#定义预编译宏
LOCAL_CFLAGS += -DSQLITE_HAS_CODEC ##该宏用于开启加密功能

#源文件
LOCAL_SRC_FILES := src/sqlite3secure.c


LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/src

#头文件目录
LOCAL_C_INCLUDES := $(LOCAL_PATH)/src

#构建静态库
include $(BUILD_STATIC_LIBRARY)


2.工程中引入wxsqlite3静态库

cocos2d-x3.x:修改cocos2d\cocos\storage\local-storage目录下的Android.mk文件如下

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_localstorage_static

LOCAL_MODULE_FILENAME := liblocalstorage

LOCAL_SRC_FILES := LocalStorage.cpp


LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/..


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../..

LOCAL_CFLAGS += -Wno-psabi
LOCAL_CFLAGS += -DSQLITE_HAS_CODEC ##该宏用于开启加密功能
LOCAL_EXPORT_CFLAGS += -Wno-psabi

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += wxsqlite3_static #引入静态库

include $(BUILD_STATIC_LIBRARY)

$(call import-module,.)
$(call import-module,external/wxsqlite3) #引入模块

cocos2d-x2.x:修改cocos2d-x-2.x\extensions目录下的Android.mk文件如下,直接把加密功能增加到引擎扩展库

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_extension_static

LOCAL_MODULE_FILENAME := libextension

LOCAL_SRC_FILES := AssetsManager/AssetsManager.cpp \
##太长,这里就省略了源文件的导入##


LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_curl_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += libwebsockets_static
LOCAL_WHOLE_STATIC_LIBRARIES += wxsqlite3_static ##引入静态库

LOCAL_CFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_EXPORT_CFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_CPPFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_EXPORT_CPPFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1

LOCAL_CFLAGS := -fexceptions
LOCAL_CFLAGS += -DSQLITE_HAS_CODEC ##该宏用于开启加密功能

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/CCBReader \
$(LOCAL_PATH)/GUI/CCControlExtension \
$(LOCAL_PATH)/GUI/CCScrollView \
$(LOCAL_PATH)/network \
$(LOCAL_PATH)/LocalStorage \
$(LOCAL_PATH)/CCArmature

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../scripting/lua/cocos2dx_support \
$(LOCAL_PATH)/../scripting/lua/lua


include $(BUILD_STATIC_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,CocosDenshion/android)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)
$(call import-module,external/libwebsockets/android)
$(call import-module,external/wxsqlite3) ##引入模块


3.Cocos2d-x引擎在Android平台下默认是通过Jni调用android sdk自带的SQLiteOpenHelper和SQLiteDatabase去创建和操作数据库的,所以在Android下使用wxsqlite3,需要进行以下几个步骤:

(1).注释LocalStorage.cpp中对于安卓平台的判断宏

【Cocos2d-x】集成wxSqlite3到Cocos2d-x


(2).修改Android.mk:Cocos2d-x对Android平台下数据库的操作封装在LocalStorageAndroid.cpp,现在修改为使用LocalStorage.cpp,使用跨平台的wxsqlite3


Cocos2d-x3.0:修改cocos2d\cocos\storage\local-storage目录下的Android.mk,删除LOCAL_SRC_FILES字段中的LocalStorageAndroid.cpp 

Cocos2d-x2.0:修改cocos2d-x-2.x\extensions目录下的Android.mk,修改LOCAL_SRC_FILES字段中LocalStorageAndroid.cpp为LocalStorage.cpp


现在编译工程,就可以了。生成数据库文件应该在/data/data/包名/files目录下,如果加密成功了,使用sqlite查看器打开应该会失败。


编译到IOS


1.打开ios工程,右键Add File,添加wxsqlite3文件夹到工程。

【Cocos2d-x】集成wxSqlite3到Cocos2d-x

只保留sqlite3.h和sqlite3secure.c的引用即可。

【Cocos2d-x】集成wxSqlite3到Cocos2d-x


2.点击工程——Build Settings——设置预编译宏(SQLITE_HAS_CODEC),开启加密功能

【Cocos2d-x】集成wxSqlite3到Cocos2d-x

3.编译成功!