关于wxsqlite3
wxsqlite3是一个轻量级的开源数据库项目,基于sqlite3,相比sqlite3增加了数据库加密功能,是用C语言实现的,跨平台。
下载wxsqlite3
下载地址:https://github.com/utelle/wxsqlite3
集成wxsqlite3到Cocos2d-x
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方法中初始化数据库时设置密码
- voidlocalStorageInit(constchar*fullpath)
- {
- if(!_initialized){
- intret=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
- char*sql_select="SELECTvalueFROMdataWHEREkey=?;";
- ret|=sqlite3_prepare_v2(_db,sql_select,-1,&_stmt_select,NULL);
- //REPLACE
- char*sql_update="REPLACEINTOdata(key,value)VALUES(?,?);";
- //DELETE
- char*sql_remove="DELETEFROMdataWHEREkey=?;";
- if(ret!=sqlITE_OK){
- printf("ErrorinitializingDB\n");
- //reporterror
- }
- _initialized=1;
- }
- } @H_41_301@
测试工程svn地址:http://code.taobao.org/svn/wxsqlite3_cocos2dx/trunk
编译到Android
1.在external\wxsqlite目录下添加Android.mk文件,文件内容如下:
2.工程中引入wxsqlite3静态库
cocos2d-x3.x:修改cocos2d\cocos\storage\local-storage目录下的Android.mk文件如下
- LOCAL_PATH:=$(callmy-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)
- $(callimport-module,.)
- $(callimport-module,external/wxsqlite3)#引入模块 @H_41_301@