Hi
I made my sqlite database and I want to read from it in game.
where should I put my database ?
I tried:
\Resources
\proj.android\src\com\example\mytest\
I cannot create and read/write sqlite when running my application on Android. In fact, it seems that it crashes on function CCFileUtils::sharedFileUtils()->fullPathForFilename()
CCLOG("[SQLClient] Here 1");
std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("activeschool.db3");
CCLOG("[SQLClient] Here 2");
What am I doing wrong? I’ve readed already many posts here, many tutorials, and I cannot find a reason to this happens. I’ve already added to manifest, but with no success.
I think the way the database is created and handled in this tutorial is too complicated. Here’s a simple and cross-platform (no #if CC_TARGET_PLATFORM, tested on iOS and Android using cocos2d-x 2.2.1 only so far) way of doing this:
And using the above class is simple enough. In your AppDelegate::applicationDidFinishLaunching() add the this line at the end Database::open(); and then use the class to execute SQL statements, something like Database::execute("YOUR SQL").
@mserougi
Thanks. May I know where you place your db file in proj.android folder?
If I understand correctly, the db file is in read-only and need to copy to writable path first. That’s the reason to check (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
For Android you would use the value returned by Context.getFilesDir() and place your SQLite database here. This usually return something like: /data/data//
What I would do to be safe is not copy your database, meaning, nothing prebuilt.
I’d let your app create the database. That way it will put it where it goes and everything will be fine. Usually data/data/your.app.package/databases/ or something. Android has built in SQLite support.
So I’d execute your create tables, inserts, etc when you start the app if the database doesn’t exist. After that is done the first time, no need to do it anymore, your will check to see if it exists and it will, so you can proceed with normal operations.
@slackmoehrle I have tried to do what you said to me but I really can not find the path to place the database file. Creating a new database really no problem at all. You can add my skype and help me? I tried to conversion my game from cocos2d-x 2.* to 3.0 but this makes me really disappointed.
@HoangTaiki - What help do you need though? Why don’t you e-mail me with what your issues are with the conversion from 2 to 3 and let me see if I can help you. jason@us.chukong-inc.com