又熟悉了一丢丢数据库的操作。来吧!
.h文件:
#ifndef __MYDATABASE_H_
#define __MYDATABASE_H_
#include "cocos2d.h"
#include "sqlite3.h"
using namespace cocos2d;
int databaseCopy(const char *src,const char *path);//拷贝数据
class Mydatabase:public Layer{
public:
virtual bool init();
static Scene *createScene();
CREATE_FUNC(Mydatabase);
Data data;
std::string getDataBaseName;//资源文件夹下面的数据库
std::string dfileName;//可读写路径下的数据库
sqlite3 *sqlite; //指向数据库的指针
void opendb(); //打开数据库
void insertdb(); //插入数据
void selectdb(); //查询数据
void updatedb(); //更新数据
void deletedb(); //删除数据
};
#endif
.cpp文件:
#include "Mydatabase.h"
bool Mydatabase::init(){
if (!Layer::init()){
return false;
}
//当程序第一次运行的时候执行resource中的文件,然后将文件拷贝到 可写入的目中中(为了跨平台)
dfileName = FileUtils::getInstance()->getWritablePath()+"tank2.sqlite3";
//data = FileUtils::getInstance()->getDataFromFile("Tank2.sqlite3");
getDataBaseName =FileUtils::getInstance()->fullPathForFilename("Tank.sqlite3");//fullPathForFilename获取resource中的文件;
if(!FileUtils::getInstance()->isFileExist(dfileName)){//如果不存在就拷贝
databaseCopy(getDataBaseName.c_str(),dfileName.c_str());
}else{
opendb();
}
insertdb();
selectdb();
updatedb();
deletedb();
return true;
}
int databaseCopy(const char *src,const char *des){ //拷贝数据
Data data = FileUtils::getInstance()->getDataFromFile(src);//获取源文件
FILE *fp = fopen(des,"wb");
fwrite(data.getBytes(),data.getSize(),1,fp);//写入
fclose(fp);//关闭文件
return 1;
}
void Mydatabase::opendb(){ //打开数据库
if (sqlite3_open(dfileName.c_str(),&sqlite)!=SQLITE_OK){
return;//打开失败
}
log("open successfully");
}
void Mydatabase::insertdb(){ //插入数据
const char *sql= "insert into Enemy(name,x,y,texture) values(?,?,?,?)";//创建插入语句
sqlite3_stmt *stmt;//集合
sqlite3_prepare(sqlite,sql,-1,&stmt,nullptr);
//这样的做法防止SQL注入式攻击
sqlite3_bind_text(stmt,1,"majianjie",-1,nullptr);//在第一个问号的位置插入数值是 “majianjie”
sqlite3_bind_int(stmt,2,40);//为x赋值
sqlite3_bind_int(stmt,3,40);//为y赋值
sqlite3_bind_text(stmt,4,"one.png",-1,nullptr);
//执行 插入语句
sqlite3_step(stmt);
log("insert successfully");
}
void Mydatabase::selectdb(){ //查询数据
sqlite3_stmt *stmt;
if (sqlite3_prepare(sqlite,"select * from Enemy where id = ? and name = ?",-1,&stmt,nullptr)!=SQLITE_OK){
return;
}
sqlite3_bind_int(stmt,1,2);//第2个参数是第几个? 第三个数是传的值
sqlite3_bind_text(stmt,2,"big",-1,NULL);//第3个参数是 传的值
//遍历
while (sqlite3_step(stmt)== SQLITE_ROW){
//更具类型获取不同的 字段
int id = sqlite3_column_int(stmt,0);
const unsigned char * name = sqlite3_column_text(stmt,1);
float x = sqlite3_column_double(stmt,2);
float y = sqlite3_column_double(stmt,3);
const unsigned char *texture = sqlite3_column_text(stmt,4);
log("id = %d, name = %s, x = %.2f, y = %.2f, texture = %s",id,name,x,y,texture);
}
log("select successfully");
}
void Mydatabase::updatedb(){ //更新数据
const char *sql ="update Enemy set name = 'majianjie' where id=1 ";
sqlite3_exec(sqlite,sql,nullptr,nullptr,nullptr);
log("update successfully");
}
void Mydatabase::deletedb(){ //删除数据
const char *sql ="delete from Enemy where id=7 ";
sqlite3_exec(sqlite,sql,nullptr,nullptr,nullptr);
log("delete successfully");
}
Scene *Mydatabase::createScene(){
Scene *scene = Scene::create();
auto layer = Mydatabase::create();
scene->addChild(layer);
return scene;
}
哪里有问题请吐槽哈。