环境说明:
cocos2d-x2.2.1
MyDB.h
#ifndef _MY_DB_
#define _MY_DB_
#include "cocos-ext.h"
#include "LocalStorage/LocalStorage.h"
USING_NS_CC_EXT;
class DB{
public:
static void init();
static void free();
static void insert();
static void del();
static void update();
static void query();
};
#endif
MyDB.cpp
#include "MyDB.h"
void DB::init(){
//初始化一个数据库,会创建一张data表,数据库文件会生成在Resource目录下
localStorageInit("my.db");
}
void DB::free(){
//释放资源
localStorageFree();
}
void DB::insert(){
//插入数据到data表
localStorageSetItem("a","1");
localStorageSetItem("b","2");
localStorageSetItem("c","3");
}
void DB::del(){
//删除数据
localStorageRemoveItem("c");
}
void DB::update(){
//更新数据
localStorageSetItem("a","2");
}
void DB::query(){
//查询数据
CCLOG("query result : %s" ,localStorageGetItem("a"));
}
1.创建一个Cocos2d-x的cpp工程
2.首先在HelloWorldScene.cpp中 #include "MyDB.h"
3.修改HelloWorldScene.cpp中的init方法实现(添加按钮,点击回调分别对DB执行不同的操作)
bool HelloWorld::init()
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(pSprite, 0);
//创建几个按钮,回调方法分别对DB执行不同的操作
//初始化
CCMenuItemFont* pInitItem = CCMenuItemFont::create("Init", this, menu_selector(HelloWorld::initDb));
pInitItem->setPosition(ccp(origin.x + 100, visibleSize.height - 100));
//增
CCMenuItemFont* pInsertItem = CCMenuItemFont::create("Insert", this, menu_selector(HelloWorld::insertDb));
pInsertItem->setPosition(ccp(origin.x + 200, visibleSize.height - 100));
//删
CCMenuItemFont* pDelItem = CCMenuItemFont::create("Delete", this, menu_selector(HelloWorld::delDb));
pDelItem->setPosition(ccp(origin.x + 100, visibleSize.height - 200));
//改
CCMenuItemFont* pUpdateItem = CCMenuItemFont::create("Update", this, menu_selector(HelloWorld::updateDb));
pUpdateItem->setPosition(ccp(origin.x + 200, visibleSize.height - 200));
//查
CCMenuItemFont* pQueryItem = CCMenuItemFont::create("Query", this, menu_selector(HelloWorld::queryDb));
pQueryItem->setPosition(ccp(origin.x + 100, visibleSize.height - 300));
//释放资源
CCMenuItemFont* pFreeItem = CCMenuItemFont::create("Free", this, menu_selector(HelloWorld::freeDb));
pFreeItem->setPosition(ccp(origin.x + 200, visibleSize.height - 300));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, pInitItem, pInsertItem, pDelItem, pUpdateItem, pQueryItem, pFreeItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
return true;
}
4.在HelloWorldScene.h中增加菜单回调的方法声明
void initDb(CCObject* psender);
void freeDb(CCObject* psender);
void insertDb(CCObject* psender);
void delDb(CCObject* psender);
void updateDb(CCObject* psender);
void queryDb(CCObject* psender);
5.在HelloWorldScene.cpp中实现菜单回调
void HelloWorld::initDb(CCObject* psender){
DB::init();
}
void HelloWorld::freeDb(CCObject* psender){
DB::free();
}
void HelloWorld::insertDb(CCObject* psender){
DB::insert();
}
void HelloWorld::delDb(CCObject* psender){
DB::del();
}
void HelloWorld::updateDb(CCObject* psender){
DB::update();
}
void HelloWorld::queryDb(CCObject* psender){
DB::query();
}
6.编译-运行,但是在编译时可能会遇到如下错误,原因是链接器没有找到sqlite的库
1.在cocos2d-x-2.2.1\external\sqlite3\libraries\win32目录下找到sqlite的动态库与静态库两个文件,把这两个文件拷贝到工程的Debug.win32目录下
2.在工程右键属性 — 配置属性 — 链接器 — 输入 — 附加依赖项 — 编辑,把sqlite3.lib添加到依赖项中
3.保存配置,重新编译工程,运行成功!