Cocos2D 에서 데이터베이스 사용 하기
데이터 베 이 스 를 사용 하 는 것 은 공식 포럼 이 든 대신 들 의 블 로그 에 상세 한 소개 가 있 고 어떤 것 은 직접 가 져 와 서 사용 할 수도 있다.
나 에 대해 서 는 더 이상 말 하지 않 겠 다.
홈 페이지 의 강좌:http://cn.cocos2d-x.org/tutorial/show?id=1921
비교적 상세 한 튜 토리 얼(개인 적 으로):http://blog.csdn.net/azhou_hui/article/details/8198603
내 가 봉 한 코드:https://code.csdn.net/snippets/613534
안 드 로 이 드 프로젝트 를 만 들 때 휴대 전화 에 데이터베이스 에 존재 하 는 내용 을 표시 할 수 없다.읽 을 수도 쓸 수도 없다.하지만 PC 에서 테스트 하 는 것 은 문제 가 없다.바로 인터넷 에서 검색 해 보 니 한 형 제 는 게임 에 들 어 갈 때 스스로 읽 었 습 니 다.즉,res 의 데이터 베 이 스 를 수 동 으로 읽 었 습 니 다.코드 를 보 니 줄 이 너무 많은 것 같 아서 다른 방법 이 있 는 지 없 는 지 보 았 다.나중에 데이터베이스 경 로 를 User default.xml 과 같은 디 렉 터 리 로 바 꾸 면 문제 가 해결 되 었 습 니 다.이렇게 간단 한 이상 코드 를 다시 쓰 지 않 았 다!
.h
#ifndef _UTILS_H_
#define _UTILS_H_
#include "cocos2d.h"
#include "../sql/sqlite3.h"
#include
using namespace std;
class Utils
{
public:
///@brief
static void initDB(const char * db) ;
///@brief
static bool tableIsExist(string name) ;
///@brief
static void createTable(string sql,string name) ;
///@brief
static void deleteTable(string sql,string name) ;
///@brief
static void insertData(string sql) ;
///@brief
static void deleteData(string sql) ;
///@brief
static void updataData(string sql) ;
///@brief
static int getDataCount(string sql) ;
///@brief
static void getDataInfo(string sql,cocos2d::CCObject * pSpend,int (*callback)(void*,int,char**,char**)) ;
///@brief
static void sortDB(string sql) ;
///@brief
static void closeDB() ;
public:
Utils();
~Utils();
private:
};
#endif
.cpp
#include "Utils.h"
#include
USING_NS_CC ;
sqlite3 * pDB = NULL ;///getWritablePath() + db ;
result = sqlite3_open(path.c_str(),&pDB) ;
if (result != SQLITE_OK)
{
CCLog( "initDB %s Failed,result:%d ,errMsg:%s
" ,db, result, errMg );
}
else
{
CCLog("initDB %s Success",db);
}
}
//@brief tableIsExist
int isExisted(void * para,int n_column,char ** column_value,char ** column_name)
{
bool * isExisted = (bool*)para ;
*isExisted = (**column_value) != '0' ;
return 0 ;
}
bool Utils::tableIsExist(string name)
{
if (pDB != NULL)
{
bool tableIsExisted ;
sqlstr = "select count(type) from sqlite_master where type='table' and name ='"+name+"'";
result = sqlite3_exec(pDB,sqlstr.c_str(),isExisted,&tableIsExisted,&errMg) ;
return tableIsExisted ;
}
return false ;
}
void Utils::createTable(string sql,string name)
{
if(!tableIsExist(name))
{
result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;
if (result != SQLITE_OK)
{
CCLog( "createTable Failed result:%d errMsg:%s
" , result, errMg );
}
}
}
void Utils::deleteTable(string sql,string name)
{
if (tableIsExist(name))
{
result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;
}
}
void Utils::insertData( string sql )
{
result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMg );
if(result != SQLITE_OK )
{
CCLog( "insertData Failed,result:%d ,errMsg:%s
" , result, errMg );
}
}
void Utils::updataData(string sql)
{
result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMg) ;
if (result != SQLITE_OK)
{
}
}
///@brief getDataCount
int loadRecordCount(void * para,int n_column,char ** column_value, char ** column_name)
{
int *count = (int*)para ;
*count = n_column ;
return 0 ;
}
int Utils::getDataCount(string sql)
{
int count = 0 ;
sqlite3_exec( pDB, sql.c_str() , loadRecordCount, &count, &errMg );
return count ;
}
void Utils::getDataInfo(string sql,cocos2d::CCObject * pSpend,int (*callback)(void*,int,char**,char**))
{
sqlite3_exec( pDB, sql.c_str() , callback, pSpend, &errMg );
}
void Utils::sortDB(string sql)
{
result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMg );
CCLog("result:%d",result) ;
}
void Utils::closeDB()
{
sqlite3_close(pDB);
}
Utils::Utils(){}
Utils::~Utils(){}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cocos2d - x 2. x 2.26 으로 업그레이드 해 야 appstore 에서 64bit 지원 에 대한 검증 을 통과 할 수 있 습 니 다.x 의 친 구 를 사용 하고 있다 면 2.26 으로 업그레이드 하 는 것 이 필요 하 다.luajit 는 64bit 를 지원 하지 않 기 때문에 ios 64bit 에 서 는 lua 만 사용 할 수 있 고 luajit...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.