sqllite 数据加密以及转换数据类型

来源:互联网 发布:linux 退出文档编辑 编辑:程序博客网 时间:2024/06/05 19:36

在HelloWorld类中

添加函数

int isExisted( void * para, int n_column, char ** column_value, char ** column_name ) { bool *isExisted_=(bool*)para; *isExisted_=(**column_value)!='0'; return 0; }
在init()中

//加密以及解密是用了himi的加密解密方法sqlite3 *pDB = NULL;            //数据库指针    char * errMsg = NULL;           //错误信息  std::string sqlstr;             //SQL指令   int result;                     //sqlite3_exec返回值  bool isExisted_;                //判断是否开始  char db[100]="";  std::string path = CCFileUtils::sharedFileUtils()->getWritablePath(); strcpy(db, path.c_str());strcat(db,"save.db");char **bResult;int aa,b;result = sqlite3_open(db, &pDB);//如果不存在及新建数据库,存在及打开  sqlstr="select count(type) from sqlite_master where type='table' and name='achievement_t'";sqlite3_exec( pDB, sqlstr.c_str() , ::isExisted, &isExisted_, &errMsg );result=sqlite3_exec( pDB, "create table achievement_t( id integer primary key autoincrement, stute string ) " , NULL, NULL, &errMsg );if(!isExisted_){result=sqlite3_exec( pDB, "create table achievement_t( id integer primary key autoincrement, stute string ) " , NULL, NULL, &errMsg );}char g[10];itoa(0,g,10);//加密std::string number=himiSaveData(g,10);CCLOG("%s",number.c_str());sqlstr=" insert into achievement_t( stute) values (  '"+number+"') "; for(int i=0;i<15;i++){result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );}//获得整个表的数据sqlite3_get_table(pDB,"select stute from achievement_t",&bResult,&aa,&b,&errMsg);CCLOG("%d",aa);for (int i=1;i<(aa+1)*b;i++){//解密前CCLOG(bResult[i]);//解密std::string n=himiParseData(bResult[i]);int a =atoi(n.c_str());    //解密后CCLOG("%d,%d",i,a);}sqlite3_close(pDB);
可以输出

表示成功了 
继续写数据库查询以及解密函数

如下

sqlite3 *pDB = NULL;//数据库指针  char * errMsg = NULL;//错误信息std::string sqlstr;//SQL指令char db[100]="";strcpy(db, path.c_str());strcat(db,"save.db");sqlite3_open(db, &pDB);sqlstr="select tfive from history_t where id=1";sqlite3_exec( pDB, sqlstr.c_str() ,DataControl::queryCallBack, count, &errMsg );sqlite3_close(pDB);DataControl::row=0;std::string n=himiParseData(count->c_str());int num =atoi(n.c_str());
有个回调函数 ,我这里是

//这里的回调函数的void* para 与上面的count相同,付给para值等同于付给count //第一个数是可以回调给上面count的数值,第二个数是列数,第三个数是列的value数值,第三个数是列的名字int DataControl::queryCallBack(void* para,int n_column,char** column_value,char** column_name){std::string *temp = (std::string*)para;*temp=(std::string)*column_value;return 0;//int *temp = (int*)para;//int count=0;//将char*类型转化成int类型//下面这样写不是很好,而且这样写就是找出一列出来然后 就知道列第二个数就是要找的数值(除名称 第一列是名称)//最好用sscanf(*column_value,"%d",&count);可达到同样效果,也比较合理,目的就是将column_value的值转成int型付给count//sscanf(*(column_name+1),"%d",&count);//*temp=count;//return 0;}
这样就能解密出来,祝你成功

下载demo地址http://download.csdn.net/detail/five50/6801737

0 0
原创粉丝点击