wxsqlite3是一个轻量级的开源数据库项目,基于sqlite3,相比sqlite3增加了数据库加密功能,是用C语言实现的,跨平台。
下载地址1:http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/
下载地址2:http://pan.baidu.com/s/11wzpk
1.在cocos2d-x引擎external目录下新建wxsqlite3\src目录
2.把wxsqlite3-3.1.1\sqlite3\secure\src目录下的.h和.c文件拷贝到wxsqlite3/src目录下
3.添加sqltesecure.c到liblocalstorage项目中。(Cocos2d-x2.x是添加到libExtensions项目)
4.设置预编译宏:sqlITE_HAS_CODEC,该宏用于开启加密功能。
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,//新密码
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,&_stmt_update,NulL); // DELETE const char *sql_remove = "DELETE FROM data WHERE key=?;"; ret |= sqlite3_prepare_v2(_db,sql_remove,&_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
1.在external\wxsqlite目录下添加AndroID.mk文件,文件内容如下:
2.工程中引入wxsqlite3静态库
cocos2d-x3.x:修改cocos2d\cocos\storage\local-storage目录下的AndroID.mk文件如下
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODulE := cocos_localstorage_staticLOCAL_MODulE_filename := liblocalstorageLOCAL_SRC_fileS := localstorage.cppLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/..LOCAL_C_INCLUDES := $(LOCAL_PATH)/../..LOCAL_CFLAGS += -Wno-psabiLOCAL_CFLAGS += -DsqlITE_HAS_CODEC ##该宏用于开启加密功能LOCAL_EXPORT_CFLAGS += -Wno-psabiLOCAL_WHolE_STATIC_liBRARIES := cocos2dx_staticLOCAL_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 := -fexceptionsLOCAL_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中对于安卓平台的判断宏
(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查看器打开应该会失败。
1.打开ios工程,右键Add file,添加wxsqlite3文件夹到工程。
只保留sqlite3.h和sqlite3secure.c的引用即可。
2.点击工程——Build Settings——设置预编译宏(sqlITE_HAS_CODEC),开启加密功能
3.编译成功!
总结以上是内存溢出为你收集整理的【Cocos2d-x】集成wxSqlite3到Cocos2d-x全部内容,希望文章能够帮你解决【Cocos2d-x】集成wxSqlite3到Cocos2d-x所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)