cocos2dx学习笔记:sqlite3封装的简单工具类

前端之家收集整理的这篇文章主要介绍了cocos2dx学习笔记:sqlite3封装的简单工具类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

---使用说明

都是静态方法,直接用即可,如下:

sql::open("PopStar");
sql::setInt(101,lv);
sql::setString("starData",starData.c_str());
sql::createTable("User","createtableUser(idinteger,nametext)");
sql::exec("deletefromUserwhereid=1");//增、删、改的语句可以直接使用这个方法
sql::exec("select*fromUser",loadRecordForString,&get);//查找的语句需要加个回调来处理数据
sql::close();


---sql.h

#ifndef_sql_H_
#define_sql_H_
#include"cocos2d.h"
#pragmacomment(lib,"sqlite3")
usingnamespacestd;
classsql{
public:
	staticvoidopen(constchar*sqlName);//打开数据库,传入数据库名称
	staticvoidexec(stringsql);//执行sql语句
	staticvoidexec(stringsql,int(*callback)(void*,int,char**,char**),void*arg);//执行sql语句,有回调
	staticvoidclose();//关闭,最好在操作完数据库后及时关闭
	//---以下是一些简单常用的方法---
	staticboolisTableExist(stringtableName);//判断该表是否存在
	staticvoidcreateTable(stringtableName,stringsql);//在不存在该表的情况下创建该表,需要传入表名、创建表的sql语句
	staticbooldeleteTable(stringtableName);
	staticintgetDataCount(stringtableName);//获取该表的行数
	//---存一些简单的数据的时候,无需手动创建表,功能类似UserDefault---
	staticvoidsetInt(intkey,intvalue);
	staticintgetInt(intkey,intdefaultValue);
	staticvoidsetString(stringkey,stringvalue);
	staticstringgetString(stringkey,stringdefaultValue);
};
#endif//_sql_H_

---sql.cpp

#include"sql.h"
#include"sqlite3\include\sqlite3.h"
#include<stdlib.h>
#pragmacomment(lib,"sqlite3")
USING_NS_CC;

sqlite3*pDB=NULL;//数据库指针
char*errMsg=NULL;//错误信息
std::stringsqlstr;//sql指令
intresult;//返回值

constchar*simpleTableNameInt="SimpleTableInt";
constchar*simpleTableNameString="SimpleTableString";

//打开数据库
voidsql::open(constchar*sqlName){
	//打开一个数据库,如果该数据库不存在,会自动创建一个
	result=sqlite3_open(sqlName,&pDB);
	if(result!=sqlITE_OK){
		CCLOG("opensqlitefail,code:%d,cause:%s\n",result,errMsg);
	}
	else{
		CCLOG("opensqlitesuccess:%s",sqlName);
	}
	//目前只有int,之后会陆续优化改进
	sqlstr=String::createWithFormat("createtable%s(keyinteger,valueinteger)",simpleTableNameInt)->_string;
	createTable(simpleTableNameInt,sqlstr);
	sqlstr=String::createWithFormat("createtable%s(keytext,valuetext)",simpleTableNameString)->_string;
	createTable(simpleTableNameString,sqlstr);
}

//执行sql语句,示例如下:
//插入:insertintoMyTable_1(name)values('擎天柱')
//删除:deletefromMyTable_1whereID=2
//修改:updateMyTable_1setname='威震天'whereID=3
voidsql::exec(std::stringsql){
	result=sqlite3_exec(pDB,sql.c_str(),NULL,&errMsg);
	if(result!=sqlITE_OK){
		CCLOG("runsqlitefail:%s,errMsg);
	}
	else{
		CCLOG("runsqlitesuccess:%s",sql.c_str());
	}
}

//执行sql语句,有回调,一般用于查询语句
voidsql::exec(stringsql,void*arg){
	result=sqlite3_exec(pDB,callback,arg,sql.c_str());
	}
}

//关闭数据库
voidsql::close(){
	sqlite3_close(pDB);
}

//isTableExist的回调函数
intisExisted(void*para,intn_column,char**column_value,char**column_name)
{
	bool*isExisted_=(bool*)para;
	*isExisted_=(**column_value)!='0';
	return0;
}
//该表是否存在
boolsql::isTableExist(std::stringtableName){
	if(pDB!=NULL){
		//判断表是否存在
		boolisTableExist;
		sqlstr="selectcount(type)fromsqlite_masterwheretype='table'andname='"+tableName+"'";
		result=sqlite3_exec(pDB,sqlstr.c_str(),isExisted,&isTableExist,&errMsg);
		returnisTableExist;
	}
	returnfalse;
}

//创建一张表,如果已存在则不创建
//示例:createtableuser(idinteger,usernametext,passwordtext)
voidsql::createTable(std::stringtableName,std::stringsql){
	if(!isTableExist(tableName)){
		result=sqlite3_exec(pDB,&errMsg);
		if(result!=sqlITE_OK){
			CCLOG("createsqlitetable%sfail,tableName,errMsg);
		}
		else{
			CCLOG("createsqlitetablesuccess:%s",tableName);
		}
	}
}

//删除一张表
boolsql::deleteTable(std::stringtableName){
	if(isTableExist(tableName)){//表存在的时候,执行删除语句
		sqlstr="droptable"+tableName;
		result=sqlite3_exec(pDB,&errMsg);
		if(result!=sqlITE_OK){
			CCLOG("deletesqlitetable%sfail,errMsg);
			returnfalse;
		}
	}
	returntrue;//能执行到最后,就说明删除成功了
}

intloadRecordCount(void*para,intn_col,char**col_value,char**col_name){
	int*count=(int*)para;
	*count=n_col;
	return0;
}
//获取该表的行数
intsql::getDataCount(std::stringtableName){
	if(isTableExist(tableName)){
		sqlstr="selectcount(*)from"+tableName;
		intcount=0;
		result=sqlite3_exec(pDB,loadRecordCount,&count,&errMsg);
		if(result!=sqlITE_OK){
			CCLOG("getsqlitetabledatacountfail,errMsg);
		}
		returncount;
	}
	return0;
}

//存int
voidsql::setInt(intkey,intvalue){
	//先删除原先的数据
	sqlstr=String::createWithFormat("deletefrom%swherekey=%d",simpleTableNameInt,key)->_string;
	exec(sqlstr);
	//再插入
	sqlstr=String::createWithFormat("insertinto%s(key,value)values(%d,%d)",key,value)->_string;
	exec(sqlstr);
}
intloadRecordForInt(void*para,char**column_name){
	int*value=(int*)para;
	*value=atoi(column_value[1]);
	return0;
}
//取int
intsql::getInt(intkey,intdefaultValue){
	intget=defaultValue;
	exec(String::createWithFormat("select*from%swherekey=%d",key)->_string,loadRecordForInt,&get);
	returnget;
}

//存string
voidsql::setString(std::stringkey,std::stringvalue){
	//先删除原先的数据
	sqlstr=String::createWithFormat("deletefrom%swherekey='%s'",simpleTableNameString,key.c_str())->_string;
	exec(sqlstr);
	//再插入
	sqlstr=String::createWithFormat("insertinto%s(key,value)values('%s','%s')",key.c_str(),value.c_str())->_string;
	exec(sqlstr);
}
intloadRecordForString(void*para,char**column_name){
	string*value=(string*)para;
	*value=column_value[1];
	return0;
}
//取string
stringsql::getString(stringkey,stringdefaultValue){
	stringget=defaultValue;
	exec(String::createWithFormat("select*from%swherekey='%s'",key.c_str())->_string,&get);
	returnget;
}

猜你在找的Cocos2d-x相关文章