2013년 11월 29일 금요일
2013년 11월 25일 월요일
Cocos2d-x IOS 프로젝트에서 Sqlite3사용하기
1. 프로젝트에 sqlite3 라이브러리 추가
2. 미리 생성된 .sqlite(또는 .db)파일을 IOS프로젝트의 Resources디렉토리에 위치시킨다.
3. 초기 구동시 DB를 복사한다.
복사방법 :
2. 미리 생성된 .sqlite(또는 .db)파일을 IOS프로젝트의 Resources디렉토리에 위치시킨다.
3. 초기 구동시 DB를 복사한다.
복사방법 :
#pragma mark -
#pragma mark Initialize DB (Resource에 있는 sqlite파일을 Document로 복사)
void initializeDB()
{
NSFileManager* fm = [NSFileManager defaultManager];
const char* dbPath = filePath("nih_DB.sqlite");
//[fm removeItemAtPath:[NSString stringWithUTF8String:dbPath] error:NULL];
BOOL fileExists = [fm fileExistsAtPath:[NSString stringWithUTF8String:dbPath]];
if(!fileExists)
{
// 참조 데이터베이스 파일 경로
NSString* origin = [[NSBundle mainBundle]pathForResource:@"nih_DB" ofType:@"sqlite"];
// Copy DB
BOOL copyDB = [fm copyItemAtPath:origin toPath:[NSString stringWithUTF8String:dbPath] error:NULL];
if(!copyDB)
{
printf("Copy failed!");
}
}
}
#pragma mark -
#pragma mark File Path
const char* filePath(char* _name)
{
NSString *name = [NSString stringWithUTF8String:_name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *_filePath = [path stringByAppendingPathComponent:name];
return [_filePath UTF8String];
}
Cocos2d-x Android 프로젝트에서 Sqlite3 사용하기
미리 생성된 DB를 사용하는 방법
1. external/sqlite3/Android.mk파일에 다음과 같은 내용을 추가한다.
1. external/sqlite3/Android.mk파일에 다음과 같은 내용을 추가한다.
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := sqlite3_static LOCAL_MODULE_FILENAME := libsqlite3 LOCAL_SRC_FILES := sqlite3.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_CFLAGS := -fno-fast-math include $(BUILD_STATIC_LIBRARY)
2. jni/Android.mk를 다음과 같이 수정한다.
3. 생성된 .sqlite(또는 .db) 파일을 프로젝트의 Resources 디렉토리에 위치시킨다.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += sqlite3_static // 추가한 코드
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)
$(call import-module,external/sqlite3) // 추가한 코드
3. 생성된 .sqlite(또는 .db) 파일을 프로젝트의 Resources 디렉토리에 위치시킨다.
4. 어플을 초기 구동시 DB를 복사한다.
복사방법:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "DbManager.h"
bool isFileExist(const char* fileName);
void copyDatabase(const char* fileName);
void DbManager::initializeDB()
{
// 프로젝트에 데이터베이스 파일이 존재하지 않으면 복사
if (isFileExist("Fortune.sqlite") == false)
{
copyDatabase("Fortune.sqlite");
}
}
#pragma mark -
#pragma mark 데이터베이스 파일 존재여부 확인
bool isFileExist(const char* fileName)
{
if (!fileName)
{
return false;
}
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath();
filePath += fileName;
FILE* file = fopen(filePath.c_str(), "r");
CCLog("%s", filePath.c_str());
if (file)
{
fclose(file);
return true;
}
return false;
}
#pragma mark -
#pragma mark copy database file
void copyDatabase(const char* fileName)
{
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
unsigned long len = 0;
unsigned char* data = NULL;
data = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "r", &len);
std::string destPath = CCFileUtils::sharedFileUtils()->getWritablePath();
destPath += fileName;
FILE* file = fopen(destPath.c_str(), "w+");
fwrite(data, sizeof(destPath.c_str()), len, file);
fclose(file);
delete[] data;
data = NULL;
}
#endif
피드 구독하기:
덧글 (Atom)

