cocos2d-x sqlite数据库为空

时间:2014-05-01 11:17:08

标签: android sqlite cocos2d-x

在cocos2d iPhone开发中首先使用sqlite数据库,我将它从资源复制到DocumentsDirectory,然后打开它。

BOOL success;

DBName = @"StageDB.sqlite";
documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentDir = [documentsPaths objectAtIndex:0];
DBPath = [documentDir stringByAppendingPathComponent:DBName];

//check if present
NSFileManager *fm = [NSFileManager defaultManager];
success=[fm fileExistsAtPath:DBPath];

if(success)
{
    NSLog(@"Already present");
}
else
{
    //Copy from bundle to DocumentsDirectory on first run. Where DB won't be available in DocumentsDirectory.
    NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"StageDB" ofType:@"sqlite"];
    NSError*error;
    success=[fm copyItemAtPath:bundlePath toPath:DBPath error:&error];

    if(success)
    {
        NSLog(@"Created successfully");
    }
}

sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK

在cocos2d-x Android开发中,我使用它而不复制,数据库为空。

pDB = NULL; //for database path
char* errMsg = NULL; //for error message
std::string sqlstr; //for sql query string
int result;
std::string dbPath = CCFileUtils::sharedFileUtils()->getWritablePath();
dbPath.append("StageDB.sqlite");
result = sqlite3_open(dbPath.c_str(),&pDB);
if (result != SQLITE_OK)
    CCLOG("OPENING WRONG, %d, MSG:%s",result,errMsg);
else
    CCLOG("result %d",result);

此选择返回零:

std::string sqlstr = "select count (type) AS cou from sqlite_master where type = 'table'";

在cocos2d-x中我必须复制数据库才能使用它吗?

1 个答案:

答案 0 :(得分:0)

对于iOS,使用<ApplicationHome>/Documents作为Purchases和Savegames的存储路径。 您可以将<ApplicationHome>/Library/Caches用于可能随时删除的临时数据。 在Cocos2d-x v3.0和v 2.2中,getWritablePath()返回:<ApplicationHome>/Documents/

对于Android,您将使用Context.getFilesDir()返回的值并将SQLite数据库放在此处。这通常会返回类似于:/data/data/<Package Name>/

的内容

std::cout您的dbPath看看它的想法是什么。

string sqlstr = "select count (type) AS cou from sqlite_master where type = 'table'";应该返回数据库中的表数。