Has anyone linked cocos2dx to a database, such as sqlite, successfully?

After go through the database-related posts in cocos2dx’s forum.

Most of posts suggest us to use CCUserDefault to store persistent data.

I thinks that it is a great approach to store small size of data, such as highest score, user’s name…etc.

However, I am going to store about 1000 maps configuration, and read this configuration file back in program when user changes his map every time.

Without SQL, I don’t know how to do it well, and I think it will be slow if I parse a xml configuration by CCUserDefault for 1000 maps every time.

So, does anyone figure out how to link a sqlite library?
There is a related post : http://www.cocos2d-x.org/boards/10/topics/1959
(Actually, I don’t know where to start to something mentioned in this post……)

Or, please let me know how you guys manipulate large size of persistent data in cocos2dx

Thanks :smiley:

Yes, I used the Poco Library for that. Used MySQL for database.

Thanks for your reply, it encourages me a lot.

I would like to know how can I add a sqlite3.so to ndk?

I downloaded the sqlite3 c*+ library from “http://www.sqlite.org/download.html”
It works fine when I test it with a dummy sql c*+ program.

Then, I tried to add lsqlite3 in “LOCAL_LDLIBS” afterlcocos2d….to include this library to my cocos2dx project

But, when I run build_native.sh, it shows that they can’t find the library :
/home/eddyxd/Android/ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/…/lib/gcc/arm-linux-androideabi/4.4.3/…/…/…/…/arm-linux-androideabi/bin/ld: cannot find -lsqlite3

I am sorry that is my first time to deal with a opensource project, can anyone give me some suggestion to add a library to the project?

I think that it is should be work because cocos2dx do the similar things in the Android.mk file…

Thanks! :slight_smile:

I found that a easiest way to include sqlite to cocos2dx game.

That is, download the source code from sqlite3 c++ api, and add sqlite3.c to Android.mk.

Then compile these code as your cocos2dx code.

Compile result is correct, but I can’t open my sql file in asset folder.

Does anyone use the same way? Is it possible caused by android permission policy for app??

If this one can work, I think it will help a lot of developers to use cocos2dx.

Thanks ^^

Asset folder is packed into .apk, which is a zip file.
So you can not open it with normal approach.

Hi, Minggo, thanks for your reply.

However, I can’t get your meaning because the CCFileUtils::getFileData() function also use File* to read data in assets to buffer.

And I have done a small test in my program as :

unsigned long readSize;
unsigned char *readData = CCFileUtils::getFileData(“db.sqlite”,“r”,&readSize);
CCLog(“first charactor = %c”,readData[0]);

It works fine~

I don’t think that we can’t read file in assets or I must misunderstand something…

Could you explain it a little more for me?

Thanks! ^^

After search several posts in StackOverflow and certain source code of CCFIleUtils, I thinks that Minggo is right!

Based on my knowledge now, I think the best way to evade this problem is to copy out the database file from assets to other fold when program started.

Another way to do it might be to place database in res/raw folder rather than assets.

I will try both methods tonight, and hope it could work ^^

Thanks!

Hi, I have tried three ways to open a sqlite file :

  1. unzip the file in asset and copy it to writable storage system, such as SD card.
    ~~> result : success, but it is totally annoying.

  2. try to access sqlite data in res/raw
    ~~> result : crash

  3. change the file extension from .sqlite to .mp3 as the reference and many posts said :
    http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/
    -> result : file is still zipped.

I would like to know if cocos2dx do something special to ask data to be compressed?

I have this question because there are a lot of posts about ndk and assets compression saying their problems are solved by this method!

I can’t understand what is the difference between my environment with them?

I am sure my database.mp3 is still zipped because I can use CCFileUtils to unpack it and read……

Could anyone can give me some suggestion?

It is a little frustrating :frowning:

  1. change the file extension from .sqlite to .mp3 as the reference and many posts said :
    http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/
    -> result : file is still zipped.

The article doesn’t say the file is not packed into .apk, it just say that, the file is not compressed, but still packed into
.apk file if you change file extension to .mp3 or others.

Hi all
I faced the same problem.
After search and try… luckily I find out the solution.

  1. use CCFileUtils::getFileData() to read the sqlite file into a char*
  2. use standard ofstrem to write the char* into a file in CCFileUtils::getWriteablePath() (in android is /data/data/xxx/xx)
  3. than you can use sqlite3_open() to open the file in step 2

Maybe some annoying… any better suggestions?
My first post, thanks ^^

use lua bind the sqlite, then export as lua script.
After that, you can open a memery db, exec all the sql script to it;

Wow, thank you so much Ivan!
It works, even on Cocos2d-x 2.0!

I can now read my SQLite database on the Android version of my game.

Mathieu Lajeunesse wrote:

Wow, thank you so much Ivan!
It works, even on Cocos2d-x 2.0!
>
I can now read my SQLite database on the Android version of my game.

is that mean,just need the first time to write the file into writeablePath ?

Does anyone have any working code to do this? I’m trying to copy an existing database as in Ivan Lo’s example but I’m running into all sorts of problems! When I read in the database in for copying using CCFileUtils::sharedFileUtils()>getFileData I only get the first bytes of the file not the whole file.
The other thing I’m trying is to just try and open the file in read only mode, I can open it on iOS but not on Android. This is the code I’m using . All I get is the Opening Wrong error.
<pre>
sqlite3 pDB = NULL;
char
errMsg = NULL;
//string sqlstr;
int result;
CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils;
unsigned long size = 0;
const char* fileName = “pronouns.mp3”;
string path = fileUtils
>fullPathForFilename(fileName);
CCLog(“s", path.c_str());

result = sqlite3\_open(path.c\_str(),&pDB);
if (result != SQLITE\_OK)
    CCLog("OPENING WRONG, d, MSG:%s”,result,errMsg);

else
CCLog(“DB Opened”);