cocos2d-x2.1.2下使用sqlite3的例子
首先,将这两个文件导入到工程当中,后面会给大家下载地址,当然也可以去官网上下载。
在HelloWorldScene.h中导入头文件:
[cpp]
#include "sqlite3.h"
#include "sqlite3.h"
之后声明如下函数和成员
[cpp]
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
//保存路径
std::string path;
//sql语句
std::string sql;
//数据库对象
sqlite3 *pdb;
//记录返回结果是否成功
int result;
//创建表
void createTable();
//插入数据
void insertData();
//查看数据
void LookUpData();
//删除数据
void deleteData();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
//保存路径
std::string path;
//sql语句
std::string sql;
//数据库对象
sqlite3 *pdb;
//记录返回结果是否成功
int result;
//创建表
void createTable();
//插入数据
void insertData();
//查看数据
void LookUpData();
//删除数据
void deleteData();
在HelloWorldScene.cpp中插入如下代码:
[cpp]
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
pdb=NULL;
CCLabelTTF* createLabel = CCLabelTTF::create("创建表", "Arial", 24);
CCMenuItemLabel* createItem = CCMenuItemLabel::create(createLabel, this, menu_selector(HelloWorld::createTable));
CCMenu* createMenu = CCMenu::create(createItem,NULL);
createMenu->setPosition(ccp(100, 300));
this->addChild(createMenu);
CCLabelTTF* insertLabel = CCLabelTTF::create("插入数据", "Arial", 24);
CCMenuItemLabel* insertItem = CCMenuItemLabel::create(insertLabel, this, menu_selector(HelloWorld::insertData));
CCMenu* insertMenu = CCMenu::create(insertItem,NULL);
insertMenu->setPosition(ccp(100, 200));
this->addChild(insertMenu);
CCLabelTTF* lookUpLabel = CCLabelTTF::create("查看数据", "Arial", 24);
CCMenuItemLabel* lookUpItem = CCMenuItemLabel::create(lookUpLabel, this, menu_selector(HelloWorld::LookUpData));
CCMenu* lookUpMenu = CCMenu::create(lookUpItem,NULL);
lookUpMenu->setPosition(ccp(100, 100));
this->addChild(lookUpMenu);
CCLabelTTF* deleteLabel = CCLabelTTF::create("删除数据", "Arial", 24);
CCMenuItemLabel* deleteItem = CCMenuItemLabel::create(deleteLabel, this, menu_selector(HelloWorld::deleteData));
CCMenu* deleteMenu = CCMenu::create(deleteItem,NULL);
deleteMenu->setPosition(ccp(200, 100));
this->addChild(deleteMenu);
return true;
}
//创建表
void HelloWorld::createTable()
{
//获取保存路径 + 保存文件名
path=CCFileUtils::sharedFileUtils()->getWritablePath() +"save.db";
std::cout<<"path"<<path<<std::endl;
//std::string sql;
result=sqlite3_open(path.c_str(),&pdb);
if(result!=SQLITE_OK)
CCLog("open database failed, number%d",result);
//创建表的方法
result=sqlite3_exec(pdb,"create table student(ID integer primary key autoincrement,name text,易做图 text)",NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("create table failed1");
sqlite3_close(pdb);
}
//插入数据
void HelloWorld::insertData()
{
result=sqlite3_open(path.c_str(),&pdb);
if(result!=SQLITE_OK)
CCLog("open database failed, number%d",result);
sql="insert into student values(1,'changmen','male')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
sql="insert into student values(2,'xiaonan','female')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
sql="insert into student values(3,'peien','male')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
&
补充:移动开发 , 其他 ,