关于Cocos2D中使用数据库
来源:互联网 发布:最强大脑 小度 知乎 编辑:程序博客网 时间:2024/06/01 13:51
就不说为什么要用到数据库了,说下自己在使用过程中遇到的问题。
数据库的使用无论是官方论坛,还是大神们的博客里都有详细的介绍,有的甚至可以直接拿过来就用。
关于我就不多说了,
官网的教程:http://cn.cocos2d-x.org/tutorial/show?id=1921
比较详细的教程(个人认为):http://blog.csdn.net/azhou_hui/article/details/8198603
我封装的代码:https://code.csdn.net/snippets/613534
在生成安卓项目的时候,在手机上不能够显示存在数据库上的内容。不能读也不能写入。但是,在PC上测试的是是没有问题的。马上到网上搜了,有个哥们是在进入游戏的时候自己读了下,也就是手动的读取了res中数据库的内容。看了看代码,感觉太多行,于是看还有没有别的方法。后来,把数据库的路径改到了与Userdefault.xml同目录,问题就解决了。既然这么简单就没有再写代码!
.h
#ifndef _UTILS_H_#define _UTILS_H_#include "cocos2d.h"#include "../sql/sqlite3.h"#include <string>using namespace std;class Utils{public:///@brief 创建一个数据库static void initDB(const char * db) ;///@brief 判断表格是否存在static bool tableIsExist(string name) ;///@brief 创建一个表格static void createTable(string sql,string name) ;///@brief 删除一个表格static void deleteTable(string sql,string name) ;///@brief 向表中添加一条数据static void insertData(string sql) ;///@brief 从表中删除一条数据static void deleteData(string sql) ;///@brief 向表中更新一条记录static void updataData(string sql) ;///@brief 获取一个记录的条数static int getDataCount(string sql) ;///@brief 获取一条记录的信息static void getDataInfo(string sql,cocos2d::CCObject * pSpend,int (*callback)(void*,int,char**,char**)) ;///@brief 给表格排序static void sortDB(string sql) ;///@brief 关闭数据库static void closeDB() ;public:Utils();~Utils();private:};#endif
.cpp
#include "Utils.h"#include <stdlib.h>USING_NS_CC ;sqlite3 * pDB = NULL ;///<数据库指针char * errMg = NULL ;///<错误信息std::string sqlstr ;///<sql语句int result ;///<sqlite3_exec返回值void Utils::initDB(const char * db){std::string path = CCFileUtils::sharedFileUtils()->getWritablePath() + db ;result = sqlite3_open(path.c_str(),&pDB) ;if (result != SQLITE_OK){CCLog( "initDB %s Failed,result:%d ,errMsg:%s\n" ,db, result, errMg ); }else{CCLog("initDB %s Success",db);}}//@brief tableIsExist的回调函数int isExisted(void * para,int n_column,char ** column_value,char ** column_name){bool * isExisted = (bool*)para ;*isExisted = (**column_value) != '0' ;return 0 ;}bool Utils::tableIsExist(string name){if (pDB != NULL){bool tableIsExisted ;sqlstr = "select count(type) from sqlite_master where type='table' and name ='"+name+"'";result = sqlite3_exec(pDB,sqlstr.c_str(),isExisted,&tableIsExisted,&errMg) ;return tableIsExisted ;}return false ;}void Utils::createTable(string sql,string name){if(!tableIsExist(name)){result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;if (result != SQLITE_OK){ CCLog( "createTable Failed result:%d errMsg:%s\n" , result, errMg );}}}void Utils::deleteTable(string sql,string name){if (tableIsExist(name)){result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;}}void Utils::insertData( string sql ){result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMg ); if(result != SQLITE_OK ) {CCLog( "insertData Failed,result:%d ,errMsg:%s\n" , result, errMg ); } }void Utils::updataData(string sql){result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;if (result != SQLITE_OK){}}///@brief getDataCount的回调函数int loadRecordCount(void * para,int n_column,char ** column_value, char ** column_name){int *count = (int*)para ;*count = n_column ;return 0 ;}int Utils::getDataCount(string sql){int count = 0 ;sqlite3_exec( pDB, sql.c_str() , loadRecordCount, &count, &errMg ); return count ;}void Utils::getDataInfo(string sql,cocos2d::CCObject * pSpend,int (*callback)(void*,int,char**,char**)){sqlite3_exec( pDB, sql.c_str() , callback, pSpend, &errMg ); }void Utils::sortDB(string sql){result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMg ); CCLog("result:%d",result) ;}void Utils::closeDB(){sqlite3_close(pDB); }Utils::Utils(){}Utils::~Utils(){}
0 0
- 关于Cocos2D中使用数据库
- 在Cocos2d-x中使用SQLite数据库
- 在Cocos2d-x中使用SQLlite数据库
- 在Quick-cocos2d-x中使用sqlite3数据库
- 关于cocos2d-html5 中使用重力感应于重力加速
- 关于 Cocos2d-x 中使用 setFrameSize 后 MenuItem 失效
- 关于Cocos2d-x中定时器的使用总结
- 关于struts中数据库的使用
- cocos2d中使用box2d
- cocos2d 中使用Kissxml
- 关于cocos2d-x2.2.3中定时器<update>的详细解释和使用
- 关于cocos2d-js中使用 ClippingNode 以及 BlendFunc 来实现遮罩
- 关于Cocos2d-x中使用自定义地图等对象的缩放与拖动的实现
- Cocos2d 中 Box2D 使用笔记
- cocos2d-x 中使用lua
- 在cocos2d中使用shader
- cocos2d-x中使用json
- Cocos2d-x 中使用多线程
- HDU 1075 What Are You Talking About Trie
- 对称密码学:02 数据加密标准(DES)
- 基于安卓手持设备的手机应用编程——Intent、Permission、Fragment
- Scheme中的流
- c#条件结构(三)----switch结构
- 关于Cocos2D中使用数据库
- Valerie d'Amy Wineh
- 欢迎使用CSDN-markdown编辑器
- CODEFORCES Flying Saucer Segments
- What happens if your
- stringstream demo
- 泛型(Generic)学习总结
- br come along
- 菜鸟使用Linux下的 vi / vim 程序编辑器