Database Help

Hello everyone.
I have ported my cocos2dx iphone game (written in cpp) to Android, using NDK and JNI. The only thing left to port, is my sqlite database controller. Dont know how to do this? So Here is my questions:

  1. How do I create a database from CPP.
  2. In what location should this be done-how to I find the path to use?

Looking forward to hear from you:)
Regards

sqlite is integrated in android sdk, and exported via java interfaces. But we cannot use it on ndk now.
There’s 3 approaches to do this:
# build and integrate libsqlite.so on ndk by yourself, use it just like libcocos2d.so
# try to link the integrated sqlite on android devices
# use CCUserDefault in cocos2d-x instead, convert database tables/fields/items into xml structure

I haven’t tried approach 1 & 2, but just used 3 instead.
Method 2 probably to be the best because google may support it in the future.

Hi Walzer and thanks a lot for your answer.
I think I will try to use the CCUserDefault approach. Would you be so kind to send a few lines about how it works? How to create the XML file, how to edit it and how to save it?
I have tried to google it but there is not much help to find.
Thanks again. :slight_smile:

You can read the source at cocos2d-x/tests/UserDefaultTest/UserDefaultTest.cpp. It looks like NSUserDefault but is a cross-platform solution.
It’s as simple as key->value mapping. You can merge your key just like:

std::string table = "high_score";
std::string field = "field_username";
int raw = 1;
char key[128] = {0};
sprintf("%s-%s-%i", table.c_str(), field.c_str(), raw);
CCUserDefault::sharedUserDefault()->setIntegerForKey("key", 1234);
// I haven't build and run these codes, just a sample for you

The weakness is you cannot benefit from SQL statements, had to write long codes to do the query actions.