就不说为什么要用到数据库了,说下自己在使用过程中遇到的问题。
数据库的使用无论是官方论坛,还是大神们的博客里都有详细的介绍,有的甚至可以直接拿过来就用。
关于我就不多说了,
官网的教程: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(){}