上一篇呢,我们介绍了SQliteStudio 这款数据库管理工具,然后我们了建立了我们自己的小型数据库。
这次呢,我们开始在cocos2d-x中,管理我们的数据
好的,上代码
头文件:TankDbLayer.h
#ifndef _TANKDBLAYER_H_
#define _TANKDBLAYER_H_
#include <sqlite3.h>
#include "cocos2d.h"
using namespace cocos2d;
class TankDbLayer:public Layer
{
protected:
sqlite3* pdb;//定义数据库对象pdb
/**
增删改查
*/
void opendb();//打开
void qierudb();//查询
void insertdb();//增加
void updatedb();//更改
void deletedb();//删除
int fileCopy(const char* srcFileName,const char* desFileName);
public:
virtual bool init();
static Scene* createScene();
CREATE_FUNC(TankDbLayer);
};
#endif
cpp: TankDbLayer.cpp
#include "TankDbLayer.h"
#include <sqlite3.h>
Scene* TankDbLayer::createScene()
{
auto scene = Scene::create();
auto layer = TankDbLayer::create();
scene->addChild(layer);
return scene;
}
bool TankDbLayer:: init()
{
if(!Layer::init())
{
return false;
}
std::string sfileName = FileUtils::getInstance()->fullPathForFilename("Tank");
std::string dfileName = FileUtils::getInstance()->getWritablePath() + "Tank";
leCopy(sfileName.c_str(),dfileName.c_str());
opendb();//打开数据库
//qierudb();//查询
insertdb();//插入
//updatedb();
// deletedb();
return true;
}
int TankDbLayer::fileCopy(const char* srcFileName,const char* desFileName)//在windows下,把数据库文件拷贝到debug里
{
Data data = FileUtils::getInstance()->getDataFromFile(srcFileName);
FILE * fp = fopen(desFileName,"wb");//以二进制打开
fwrite(data.getBytes(),1,data.getSize(),fp);
fflush(fp);
fclose(fp);
return 1;
// FILE *fp = fopen(srcFileName,"r+");
}
void TankDbLayer::opendb()//打开这个数据库
{
log("open");
//std::string filename = FileUtils::getInstance()->fullPathForFilename("Tank");//resource目录下
std::string filename = FileUtils::getInstance()->getWritablePath() + "Weapen";//拷贝后的目录
pdb =nullptr;
if(sqlite3_open(filename.c_str(),&pdb)!= SQLITE_OK)
{
return ;
}
log("%p",pdb);
}
void TankDbLayer::qierudb()//查询
{
log("qierudb");
sqlite3_stmt* stmt = nullptr;
if(sqlite3_prepare(pdb,"select * from Enemy where id = ? and name = ?",-1,&stmt,NULL) !=SQLITE_OK)
{
return ;
}
sqlite3_bind_int(stmt,1,2);
sqlite3_bind_text( stmt, 2,"big", -1 ,NULL);
log("1111");
while (sqlite3_step(stmt) == SQLITE_ROW)
{
log("222222");
int id = sqlite3_column_int(stmt,0);
log("%d",id);
const unsigned char* name = sqlite3_column_text(stmt,1);
log("%s",name);
}
}
void TankDbLayer::insertdb()//插入
{
log("insertdb");
char *sql = "insert into Enemy(name,x,y,Texture) values(?,?,?,?)";
sqlite3_stmt* stmt = nullptr;
sqlite3_prepare(pdb,sql,-1,&stmt,nullptr);
sqlite3_bind_text(stmt,1,"mmmm",-1,nullptr);
sqlite3_bind_int(stmt,2,480);
sqlite3_bind_int(stmt,3,320);
sqlite3_bind_text(stmt,4,"Desktop.png",-1,nullptr);
sqlite3_stmt* stmt1 = nullptr;
sqlite3_prepare(pdb,sql,-1,&stmt1,nullptr);
sqlite3_bind_text(stmt1,1,"oooo",-1,nullptr);
sqlite3_bind_int(stmt1,2,480);
sqlite3_bind_int(stmt1,3,320);
sqlite3_bind_text(stmt1,4,"Desktop.png",-1,nullptr);
sqlite3_step(stmt1);
sqlite3_step(stmt);
log("the end");
}
void TankDbLayer::updatedb()//改
{
//sqlite3_stmt* stmt = nullptr;
//char *sql = "update Enemy set name = 'jkcma' where id = ?";
//sqlite3_prepare(pdb,sql,-1,&stmt,nullptr);
//sqlite3_bind_int(stmt,1,3);
//sqlite3_step(stmt);
const char* sql = "update Enemy set name = 'jkamaaa' where name = 'jkcm'";
sqlite3_exec(pdb,sql,nullptr,nullptr,nullptr);
}
void TankDbLayer::deletedb()
{
//删除列的方式有很多,我们可以通过列的id,name 等任何项来删除某一列
// sqlite3_stmt* stmt = nullptr;
// char *sql = "delete from Enemy where name = ? ";
// sqlite3_prepare(pdb,sql,-1,&stmt,nullptr);
// sqlite3_bind_text(stmt,1,"jkamaaa",-1,nullptr);
// sqlite3_step(stmt);
const char * sql = "delete from Enemy where id = 12 ";
sqlite3_exec(pdb,sql,nullptr,nullptr,nullptr);
}
可以实时刷新你的数据库,发现数据的改变
今天就说到这里,看后有什么感想或者建议欢迎大家留言或者qq联系:3031260489