cocos2d-x sqlite的使用,数据库操作的封装

本文介绍了一种针对Cocos2d-x游戏开发框架的SQLite数据库封装方案,该方案支持Key-Value形式的数据存储,并提供了加密选项。通过提供的几个核心接口,开发者能够方便地进行数据库的打开、关闭、存取及删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From: http://blog.csdn.net/fansongy/article/details/8922054

本篇博客出自阿修罗道,转载请注明出处:http://blog.csdn.net/fansongy/article/details/8922054

    移动平台要用到sqllite,发现cocos2d-x中没有专门的类对接。为了以后省事,就自己写了个封装。

    数据库的形式是Key-Value。如果某种类的数据项过多,可以将大类作为表名,以便将来的拓展。

    加密方面采用的是数据项加密。加密方式是DES加密。


对外接口我留了以下几个:

  1. public:  
  2.     /* 
  3.      * 打开数据库。 
  4.      * 
  5.      * tableName 为创建的表名,自检是否存在 
  6.      * changedKey 为变更的密钥,默认为字段SEC_KEY,也可更改。 
  7.      * 
  8.      * 返回为SQLITE_XXX标准宏 
  9.      * by fansy 2013-5-13 
  10.      */  
  11.     int openDB(std::string dbName,std::string tableName,std::string changedKey = "");  
  12.     //关闭数据库   
  13.     void closeDB();  
  14.   
  15.     /* 
  16.      * 存数据,自检是否存在,不会重复。最后一个变量表示是否加密 
  17.      * 
  18.      * 返回为SQLITE_XXX标准宏 
  19.      * by fansy 2013-5-13 
  20.      */  
  21.     int setValue(std::string key,std::string value,bool isImportantValue = DEFULAT_SECURITY);  
  22.     int setValue(std::string key,int value,bool isImportantValue = DEFULAT_SECURITY);  
  23.     int setValue(std::string key,long long value,bool isImportantValue = DEFULAT_SECURITY);  
  24.       
  25.   
  26.     /* 
  27.      * 取数据,变量结果存入Value中,最后一个变量表示是否加密 
  28.      * 
  29.      * 返回为SQLITE_XXX标准宏 
  30.      * by fansy 2013-5-13 
  31.      */  
  32.     int getValue(std::string key,std::string& value,bool isImportantValue = DEFULAT_SECURITY);  
  33.     int getValue(std::string key,int& value,bool isImportantValue = DEFULAT_SECURITY);  
  34.     int getValue(std::string key,long long& value,bool isImportantValue = DEFULAT_SECURITY);  
  35.   
  36.     /* 
  37.      * 删除数据 
  38.      * 
  39.      * 返回为SQLITE_XXX标准宏 
  40.      * by fansy 2013-5-13 
  41.      */  
  42.     int deleteValue(const std::string key,bool isImportantValue = DEFULAT_SECURITY );  
  43.       
public:
	/*
	 * 打开数据库。
	 *
	 * tableName 为创建的表名,自检是否存在
	 * changedKey 为变更的密钥,默认为字段SEC_KEY,也可更改。
	 *
	 * 返回为SQLITE_XXX标准宏
	 * by fansy 2013-5-13
	 */
	int openDB(std::string dbName,std::string tableName,std::string changedKey = "");
	//关闭数据库
	void closeDB();

	/*
	 * 存数据,自检是否存在,不会重复。最后一个变量表示是否加密
	 *
	 * 返回为SQLITE_XXX标准宏
	 * by fansy 2013-5-13
	 */
	int setValue(std::string key,std::string value,bool isImportantValue = DEFULAT_SECURITY);
	int setValue(std::string key,int value,bool isImportantValue = DEFULAT_SECURITY);
	int setValue(std::string key,long long value,bool isImportantValue = DEFULAT_SECURITY);
	

	/*
	 * 取数据,变量结果存入Value中,最后一个变量表示是否加密
	 *
	 * 返回为SQLITE_XXX标准宏
	 * by fansy 2013-5-13
	 */
	int getValue(std::string key,std::string& value,bool isImportantValue = DEFULAT_SECURITY);
	int getValue(std::string key,int& value,bool isImportantValue = DEFULAT_SECURITY);
	int getValue(std::string key,long long& value,bool isImportantValue = DEFULAT_SECURITY);

	/*
	 * 删除数据
	 *
	 * 返回为SQLITE_XXX标准宏
	 * by fansy 2013-5-13
	 */
	int deleteValue(const std::string key,bool isImportantValue = DEFULAT_SECURITY );
	


    首先,打开数据库,打开表应该是一个需求,就写到一起了。这里可以更改加密的密钥,不过要注意,密钥长度为16时系统自动使用3次DES加/解密,超过16字节后只取前24字节;为大于等于8小于24时使用标准DES加/解密,其它返回失败。所以建议使用16位。

    关闭倒是没什么可说的。

    存取的设计也比较简明。getValue和setValue就是读和取相应key的Value。重载了三个函数,分别是string、int、和int64的。第三个参数表示是否需要加密。DEFAULT_SECURITY这个字段表示默认是否采用加密。可以在测试的过程中将默认加密关上,发布时在打开。

    删除数据就是一个键来删除。

    这里有个要注意的地方。这套接口中其实是没有“表”这个概念的。可以简单的理解为所有的数据都是存在同一个表中的。即“openDB”时输入的默认表。

    但这个结构是可以拓展的。比如随着游戏的复杂性增加,需要有背包。背包的信息需要单独存入一个表中。这时,在set\get Vaule时可以传入“player:maxCount”。通过对 “ :”字符的检查,来判断键是否是在默认的表中。不是,则在相应的表中操作相应的值。如果有个新用户,就新建一个数据库好了。这样即使总增加新的属性,也能灵活的存取,不受先前表结构的影响。


大体实现如下:

  1. int DataUtil::openDB(std::string dbName,std::string tableName,std::string changedKey/* = ""*/)  
  2. {  
  3.     int result = 1;  
  4.     result = DataUtil::Instance()->initDB(dbName.c_str(),changedKey);  
  5.     if (result == SQLITE_OK)  
  6.     {  
  7.         result = DataUtil::createTable(tableName);  
  8.         if (result == SQLITE_OK)  
  9.         {  
  10.             m_tableName = tableName;  
  11.         }  
  12.     }  
  13.     return result;  
  14. }  
  15.   
  16. int DataUtil::setValue(std::string key,std::string value,bool isImportantValue /* = true */)  
  17. {  
  18.     int result = 1;  
  19.     std::string foreData;  
  20.     DataUtil::Instance()->getValue(m_tableName,key,foreData,isImportantValue);  
  21.     if (foreData.empty())  
  22.     {  
  23.         result = DataUtil::Instance()->insertValue(m_tableName,key,value,isImportantValue);  
  24.     }  
  25.     else  
  26.     {  
  27.         result = DataUtil::Instance()->updateValue(m_tableName,key,value,isImportantValue);  
  28.     }  
  29.     return result;  
  30. }  
  31. int DataUtil::setValue(std::string key,int value,bool isImportantValue /* = true */)  
  32. {  
  33.     char res[10];  
  34.     _itoa_s(value,res,10);  
  35.     return DataUtil::Instance()->setValue(key,res,isImportantValue);  
  36. }  
  37.   
  38. int DataUtil::setValue(std::string key,long long value,bool isImportantValue /* = true */)  
  39. {  
  40.     char res[20];  
  41.     //_ltoa_s(value,res,10);   
  42.     sprintf_s(res,"%%I64d",value);  
  43.     return DataUtil::Instance()->setValue(key,res,isImportantValue);  
  44. }  
  45.   
  46. int DataUtil::getValue(std::string key,std::string &value,bool isImportantValue /* = true */)  
  47. {  
  48.     int result = 1;  
  49.     std::string tmpValue;  
  50.     result = DataUtil::Instance()->getValue(m_tableName,key,tmpValue,isImportantValue);  
  51.     if (result == SQLITE_OK)  
  52.     {  
  53.         value = tmpValue;  
  54.     }  
  55.     return result;  
  56. }  
  57. int DataUtil::getValue(std::string key,int& value,bool isImportantValue /* = true */)  
  58. {  
  59.     std::string tempValue;  
  60.     int result = DataUtil::Instance()->getValue(key,tempValue,isImportantValue);  
  61.     if (result == SQLITE_OK)  
  62.     {  
  63.         value = atoi(tempValue.c_str());  
  64.     }  
  65.     return result;  
  66. }  
  67. int DataUtil::getValue(std::string key,long long& value,bool isImportantValue /* = true */)  
  68. {  
  69.     std::string tempValue;  
  70.     int result = DataUtil::Instance()->getValue(key,tempValue,isImportantValue);  
  71.     if (result == SQLITE_OK)  
  72.     {  
  73.         value = atol(tempValue.c_str());  
  74.     }  
  75.     return result;    
  76. }  
  77.   
  78. int DataUtil::deleteValue(const std::string key,bool isImportantValue /* = DEFULAT_SECURITY */ )  
  79. {  
  80.     return DataUtil::Instance()->deleteValue(m_tableName,key,isImportantValue);  
  81. }  
int DataUtil::openDB(std::string dbName,std::string tableName,std::string changedKey/* = ""*/)
{
	int result = 1;
	result = DataUtil::Instance()->initDB(dbName.c_str(),changedKey);
	if (result == SQLITE_OK)
	{
		result = DataUtil::createTable(tableName);
		if (result == SQLITE_OK)
		{
			m_tableName = tableName;
		}
	}
	return result;
}

int DataUtil::setValue(std::string key,std::string value,bool isImportantValue /* = true */)
{
	int result = 1;
	std::string foreData;
	DataUtil::Instance()->getValue(m_tableName,key,foreData,isImportantValue);
	if (foreData.empty())
	{
		result = DataUtil::Instance()->insertValue(m_tableName,key,value,isImportantValue);
	}
	else
	{
		result = DataUtil::Instance()->updateValue(m_tableName,key,value,isImportantValue);
	}
	return result;
}
int DataUtil::setValue(std::string key,int value,bool isImportantValue /* = true */)
{
	char res[10];
	_itoa_s(value,res,10);
	return DataUtil::Instance()->setValue(key,res,isImportantValue);
}

int DataUtil::setValue(std::string key,long long value,bool isImportantValue /* = true */)
{
	char res[20];
	//_ltoa_s(value,res,10);
	sprintf_s(res,"%%I64d",value);
	return DataUtil::Instance()->setValue(key,res,isImportantValue);
}

int DataUtil::getValue(std::string key,std::string &value,bool isImportantValue /* = true */)
{
	int result = 1;
	std::string tmpValue;
	result = DataUtil::Instance()->getValue(m_tableName,key,tmpValue,isImportantValue);
	if (result == SQLITE_OK)
	{
		value = tmpValue;
	}
	return result;
}
int DataUtil::getValue(std::string key,int& value,bool isImportantValue /* = true */)
{
	std::string tempValue;
	int result = DataUtil::Instance()->getValue(key,tempValue,isImportantValue);
	if (result == SQLITE_OK)
	{
		value = atoi(tempValue.c_str());
	}
	return result;
}
int DataUtil::getValue(std::string key,long long& value,bool isImportantValue /* = true */)
{
	std::string tempValue;
	int result = DataUtil::Instance()->getValue(key,tempValue,isImportantValue);
	if (result == SQLITE_OK)
	{
		value = atol(tempValue.c_str());
	}
	return result;	
}

int DataUtil::deleteValue(const std::string key,bool isImportantValue /* = DEFULAT_SECURITY */ )
{
	return DataUtil::Instance()->deleteValue(m_tableName,key,isImportantValue);
}

    如有大家觉得不恰当的地方,欢迎留言共同讨论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值