A simple Tutorial How to use SQLite in cocos2d-x ……(Tutorial by YUYE)

splite3 in my project not work on android. Help me yuye liu

I can help you on monday.
Tung Luong wrote:

splite3 in my project not work on android. Help me yuye liu

Yuye Liu can I ask how to open previously created database? How to add sqlite database to project and use it in game? Because if I am right you are creating a new blank database with C++. But I am asking how to use already created database?

Thanks for your tutorial and I am able to work on sqlite.
After some work, I found that using CppSQLite3 (c++ wrapper for sqlite) is better for handling existing db.

proj.win32 is OK but proj.android not work (only sqlite3)
Help me please! yuye liu

The db file should be copied to writeable folder. i don’t know why???
In my project:

std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("dict.db");

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
        dbPath  = CCFileUtils::sharedFileUtils()->getWritablePath();
        dbPath  += "/dict.db";

        FILE* file = fopen(dbPath.c_str(), "r");
        if (file == nullptr)
        {
            unsigned long size;
            const char* data = (char*) CCFileUtils::sharedFileUtils()->getFileData("dict.db", "rb", &size);
            file = fopen(dbPath.c_str(), "wb");
            fwrite(data, size, 1, file);
            CC_SAFE_DELETE_ARRAY(data);
        }
        fclose(file);
#endif

Tung Luong wrote:

proj.win32 is OK but proj.android not work (only sqlite3)
Help me please! yuye liu

Done.
zhu tang. Thankyou so much!

Thanks a lot.
zhu tang wrote:

The db file should be copied to writeable folder. i don’t know why???
In my project:
>

> std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("dict.db");
>       
> #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
>       dbPath  = CCFileUtils::sharedFileUtils()->getWritablePath();
>       dbPath  += "/dict.db";
> 
>       FILE* file = fopen(dbPath.c_str(), "r");
>       if (file == nullptr)
>       {
>           unsigned long size;
>           const char* data = (char*) CCFileUtils::sharedFileUtils()->getFileData("dict.db", "rb", &size);
>           file = fopen(dbPath.c_str(), "wb");
>           fwrite(data, size, 1, file);
>           CC_SAFE_DELETE_ARRAY(data);
>       }
>       fclose(file);
> #endif
> 

>

Tung Luong wrote:
> proj.win32 is OK but proj.android not work (only sqlite3)
> Help me please! yuye liu

a little bit confusing to apply it on cross platform code.
would you please revise the sample code for the android part? thank you for help

ok , I’ll do it for you . thank you for your opinions.
winipcfg exe wrote:

a little bit confusing to apply it on cross platform code.
would you please revise the sample code for the android part? thank you for help

Need some help on android project.
Say if we have an existing DB file named data.db3, where should we put the file under folder “proj.android”? Thank you for help.

if you wan’t save database in proj.android/res/ it will be read only. Also you can copy it and save in writable path (you can get it from CCFileManager?)

There is no CCFileManager in Cocos2d-x or I can’t find one.

Can somebody share knowledge how he/she has got this to work on Android?

I’m trying with these steps,
first - code for Android database,
than code for opening database
and then for searching - all from examples in first post, but it seems that my database is empty. This line CCLog(“row is %d,column is %d”,r,c); gives me that there’re 0 rows and 0 columns. My database has extension sqlite and I build it in SQLite Manager add-on for Firefox. I’ve placed it in Resource folder. It has one row and 7 columns - I’m trying with small example, just to get it work. Can somebody help?

I’ve got this to work. I’m working with two databases with .sqlite extenstion. I’ve probably had problem with saving dbPath to global variable, and when result=sqlite3_open(path.c_str(),&pdb); was executed it created new empty database, so that’s why there was 0 rows and 0 columns.

How do I can import a big data? In native Android, I can import database from CSV file through SQLManager. How about in Cocos2dx? Thanks

@lolyoshi you must learn what is the JNI for android.

@yuye I run don’t have error but when i open db file. I don’t find student table. Can you help me?

Hi YuYe,

In your example, one section is “asserting data” Maybe change that to “Inserting data”?

asserting = “state a fact or belief confidently and forcefully.”

This is a great tutorial. Just trying to help with some wording :slight_smile:

Also, I could give you an example on updating values as well if you wanted to include that.

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\

But Everytime my app crash ! :frowning:

Where should I put ?

PS: I even give 777 permission to mydatabse file.

Thanks in advanced
:slight_smile:

Oh. I found the solution here:

http://www.cocos2d-x.org/forums/6/topics/16534